Link to home
Start Free TrialLog in
Avatar of earngreen
earngreenFlag for United States of America

asked on

Cursor with multiple inserts

Open in new window

l_sOwner VARCHAR2(30);

I need to insert into multiple tables reusing the same cursor and wondered if anyone could provide the best approach something similar to this. I am unable to use temporary tables.

l_sName VARCHAR2(30);
l_sType VARCHAR2(19);
CURSOR cur IS SELECT owner, object_name name, object_type type FROM all_objects;
BEGIN
dbms_output.put_line('Before CURSOR OPEN: ' || systimestamp);
OPEN cur;
dbms_output.put_line('Before LOOP: ' || systimestamp);
LOOP
FETCH cur INTO l_sOwner, l_sName, l_sType;
IF cur%NOTFOUND THEN
EXIT;
END IF;
INSERT INTO temp values (l_sOwner, l_sName, l_sType);
END LOOP;
LOOP
FETCH cur INTO l_sOwner, l_sName, l_sType;
IF cur%NOTFOUND THEN
EXIT;
END IF;
INSERT INTO temp22 values (l_sOwner, l_sName, l_sType);
END LOOP;
 
LOOP
FETCH cur INTO l_sOwner, l_sName, l_sType;
IF cur%NOTFOUND THEN
EXIT;
END IF;
INSERT INTO temp33 values (l_sOwner, l_sName, l_sType);
END LOOP;
 
 
 
 
CLOSE cur;
dbms_output.put_line('After CURSOR CLOSE: ' || systimestamp);

COMMIT;
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Did you actually get an answer to your previous question or did you just accept the last post to get around the new question lock?
or use a single multi-table insert


INSERT ALL
  INTO temp
VALUES (owner, object_name, object_type)
  INTO temp22
VALUES (owner, object_name, object_type)
  INTO temp33
VALUES (owner, object_name, object_type)
    SELECT owner, object_name, object_type FROM all_objects
Avatar of earngreen

ASKER

slightwv,

I had to use the merge for something else and it worked great. So I answered the question with the solution used
That worked. Thx
just curious, did you try the multi-table insert?  It should be much more efficient since it's one sql instead of

1 + 3 * numrows

and eliminated sql to pl/sql context switches