Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Compare two cursors and Insert or Update

Posted on 2006-10-31
Medium Priority
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.

Question by:aaba646
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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) :

  SELECT col1A, col21, col3A, col4A
    FROM abc) a
ON (xyz.col1B = a.col1A AND xyz.col2B = a.col2A)
  UPDATE SET xyz.col3B = a.col3A, xyz.col4B = a.col4A
  INSERT (xyz.col1B, xyz.col2B, xyz.col3B, xyz.col4B)
  VALUES (a.col1A, a.col2A, a.col3A, a.col3A)
LVL 32

Expert Comment

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.

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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…
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

719 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