Solved

Compare two cursors and Insert or Update

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

825 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