I’d like to update my employee database to set the batch_id to “P-1000” if the employee is a plumber.
Instead of creating 5 separate (and slow) queries, I want to:
- set each index of the array is a table name
- loop through the array
- update the table using the string value at the string index
I can’t find anything even remotely like the concept I’m proposing here, which leads me to think I’m barking up the wrong tree entirely.
None the less, here is the psuedocode owhat I’m trying to do.
declare
type array_type is table of varchar2(100) index by binary_integer;
dmt_tables array_type;
begin
--fill dm_employeeTables array
dm_employeeTables(0) := 'dm_address';
dm_employeeTables(1) := 'dm_communications';
dm_employeeTables(2) := 'dm_identifier';
dm_employeeTables(3) := 'dm_name';
dm_employeeTables(4) := 'dm_qualifications';
-- loop through tables
for i in dm_employeeTables.FIRST .. dmt_tables.LAST
loop
update dm_employeeTables(i) a
set employee_batch_id = 'P-1000'
where a.employee_type = 'PLUMBER';
i=i+1;
end loop;
end;
You would need to use dynamic SQL. Something like
Now, from a good coding standpoint, you probably want to declare a new local variable (i.e.
l_sql_stmt) that you populate with the SQL statement you want to execute and then pass toEXECUTE IMMEDIATE. That allows you to do things like log the SQL statement (or print it out) before you execute the SQL statement. That generally makes debugging much easier.