Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compare two cursors and Insert or Update

Posted on 2006-10-31
2
Medium Priority
?
996 Views
Last Modified: 2008-01-09
Hi ,

I have cursor1 which has col1A,col2A,col3A,col4A and cursor2 with col1B,col2B,col3B,col4B
Now i have to compare each record from cursor1 with corresponding fields with cursor2 records and if found then update and if not found insert the new record into table xyz.

The cursor1 would bring the records from table abc whereas cursor2 would bring the records from xyz table.

Thanks
0
Comment
Question by:aaba646
2 Comments
 
LVL 6

Accepted Solution

by:
hans_vd earned 2000 total points
ID: 17841776
You can do it in 1 statement (modify the ON clause so that the fields that should correspond are in it) :

MERGE INTO xyz
USING (
  SELECT col1A, col21, col3A, col4A
    FROM abc) a
ON (xyz.col1B = a.col1A AND xyz.col2B = a.col2A)
WHEN MATCHED THEN
  UPDATE SET xyz.col3B = a.col3A, xyz.col4B = a.col4A
WHEN NOT MATCHED THEN
  INSERT (xyz.col1B, xyz.col2B, xyz.col3B, xyz.col4B)
  VALUES (a.col1A, a.col2A, a.col3A, a.col3A)
0
 
LVL 32

Expert Comment

by:awking00
ID: 17844633
Your question is a little ambiguous. Comparing two cursors can be a performance nightmare having to fetch a row from the first cursor then compare it to all of the rows from the second cursor and repeating this for all of the rows in the first cursor. Your solution might be as simple as using the merge that hans_vd shows, although I suspect that there are numerous where clauses in the select statements of your cursors that may complicate things. Post the select statements for your cursors and the update and insert statements here and we can see if there is an easier means of accomplishing your intent.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

876 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