Solved

Compare two cursors and Insert or Update

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

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

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

746 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

16 Experts available now in Live!

Get 1:1 Help Now