Solved

Merging Cursors in PL/SQL

Posted on 2007-04-10
8
7,222 Views
Last Modified: 2013-12-18
Is there a way to merge two or more cursors together into a single cursor in Oracle 10g PL/SQL?

I have multiple cursors that contain thousands of records.  Each cursor has an ID that ties the data together.  I then want to merge all of the cursors into a single cursor so that I can then perform further processing on each record in the cursor.  

I need to do this in PL/SQL within Oralce 10g.

Any ideas?
0
Comment
Question by:ccorrente
8 Comments
 
LVL 16

Expert Comment

by:MohanKNair
Comment Utility
There is no direct way to merger cursors. However it is possible to open both cursors

declare
c1t c1%rowtype;
c2t c2%rowtype;
BEGIN
open c1;
open c2;
LOOP
fetch c1 into c1t;
--  code exit conditions here
fetch c2 into c2t;
--  code exit conditions here
-- program logic
END LOOP;
0
 
LVL 17

Expert Comment

by:NicksonKoh
Comment Utility
Hi ccorrente,

If the SQLs in the 2 cursors are actually quite similar, another possible way is to write another cursor that unions the SQL in the 2 cursor or have a SQL with the conditions that will satisfy both cursors. Take note of any difference in the colmns that is selected in the 2 cursors and you need to adjust this merge SQL according such that they will contain the same sets of columns.

UNION method:
(Select ..... ) UNION (Select ....)

Merge condition :
Select ..
FROM ...
WHERE (conditions from cursor1)
OR (conditions from cursor2)

Which method is workable would depend on the kind of SQL that you have in the cursor.

Cheers,
NicksonKoh
0
 
LVL 4

Expert Comment

by:ramumorla
Comment Utility
This depends on the SQL of your 2 cusors, can u give me a idea , how these sql look like?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 21

Accepted Solution

by:
oleggold earned 500 total points
Comment Utility
Union is a nice thing but in 10G You have also an option of using the CURSOR TAG LIKE THIS:
CURSOR curs_orders IS
      SELECT order_number,
             CURSOR ( SELECT order_line_amt
                        FROM order_lines ol
                       WHERE ol.order_number = orders.order_number )
        FROM orders;
Merge Your cusor on Your key like in this example the key is the order_number
0
 
LVL 21

Expert Comment

by:oleggold
Comment Utility
NOT ONLY THIS LOOKS MORE ELEGANT AS IT SEEMS TO ME ,THIS IS ALSO MUCH MORE EFFICIENT,INSTEAD OF FETCHING 1000 ROWS PER EACH CURSOR WITH 2 SELECTS AND UNION/MAY BE SOME NESTED LOOP JOIN ON THAT IT WILL FETCH ONLY THE FIRST SELECT + ONLY RELEVANT ROWS FOR THE SECOND SELECT.
0
 
LVL 3

Expert Comment

by:biswaranjan_rath
Comment Utility
But I would suggest gather the queries of the cursor and create a NEW cursor using those queries. The advantage of this will be: a) it is simple to understand b) easy to implement and to work with c) comparatively take less memory space.
And with cursor merging there are few limitations like: 1) Result cursor may have huge data since it will merge the other cursor data and have cartesian product. Evene if there is not cartesian product, still I assume the resultset will be huge. 2) merging will complicate the understanding. 3) it will be error-prone.

If you still want to go for merging, then there are a lot of ways as other friends mentioned above.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now