1

I have a procedure in which 100 tables have to be updated one by one. All tables have the same column to be updated. For improving the performance I am trying to use Execute Immediate with FORALL but I am getting a lot of compilation errors.

Is it syntactically possible to update 100 different tables inside a FORALL statement using Execute immediate.

My code looks something like this.

Declare
  TYPE u IS TABLE OF VARCHAR2(240) INDEX BY BINARY_INTEGER;
      Table_List u;
FOR somecursor IN (SELECT variable1, variable2 FROM SomeTable) 
    LOOP 
        BEGIN 
            Table_List(1) := 'table1'; 
            Table_List(2) := 'table2'; 
            ......
            ......
            table_list(100):= 'table100';
       FORALL i IN Table_List.FIRST .. Table_List.LAST 
           EXECUTE IMMEDIATE 'UPDATE :1 SET column = :3 WHERE column = :2' 
             USING Table_List(i), somecursor.variable1, somecursor.variable2 ; 
     end loop;

I hope people can understand what I am trying to do through this code. If something is big time wrong please suggest me what exactly is the syntax and if it can be done in some other efficient way also.

Thanks a lot for all the help which comes my way.

2 Answers 2

1

(1) No, you can't use a bind variable for the table name.

(2) When you're using EXECUTE IMMEDIATE, this implies Dynamic SQL - but FORALL requires that only one statement to be executed. As soon as you specify a different table, you're talking about a different statement (regardless of whether the tables' structures happen to be equivalent or not).

You're going to have to do this in an ordinary FOR loop.

2
  • But doing it in an ordinary for loop takes a lot of time and causes session wait issues. Can you suggest any other way for updating 100 tables one by one, with the same type of query.
    – Arunabh
    Oct 16, 2012 at 10:04
  • Yes - unwind the loop, and have 100 UPDATE statements. That's the best you're going to get. The basic reason behind the performance issue is the fact that you're trying to run 100 different update statements. You then need to work out why each of the updates is taking so long, and tune them individually. Oct 17, 2012 at 1:37
0

Just a guess, but I don't think you can use a bind variable as a table name. Have you tried:

EXECUTE IMMEDIATE 'UPDATE ' || Table_List(i) || ' SET column = :2 WHERE column = :3' ...
1
  • I just tried this but got so many compilation errors...Any suggestions?
    – Arunabh
    Oct 16, 2012 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.