Solved

Compare two cursors and Insert or Update

Posted on 2006-10-31
2
987 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

863 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

22 Experts available now in Live!

Get 1:1 Help Now