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;
earngreenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
You would need to close and open the cursor every time.  Cursors are forward only.

Why not do all 3 inserts in the one loop?
loop
  fetch
  insert1
  insert2
  insert3
end loop

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightwv (䄆 Netminder) Commented:
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?
sdstuberCommented:
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
SolarWinds® VoIP and Network Quality Manager(VNQM)

WAN and VoIP monitoring tools that can help with troubleshooting via an intuitive web interface. Review quality of service data, including jitter, latency, packet loss, and MOS. Troubleshoot call performance and correlate call issues with WAN performance for Cisco and Avaya calls

earngreenAuthor Commented:
slightwv,

I had to use the merge for something else and it worked great. So I answered the question with the solution used
earngreenAuthor Commented:
That worked. Thx
sdstuberCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.