Home > Oracle Database / Applications Tips > Oracle database administrator > Update a referenced column in a MERGE statement
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE ADMINISTRATOR

Update a referenced column in a MERGE statement


Ravinder Bhalla
03.08.2005
Rating: -4.36- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


One of the most powerful features in Oracle 9i and above is the MERGE statement, which lets a single SQL statement either conditionally insert or update a table by selecting rows from another table. If the row already exists, an update is performed; otherwise, an insert is performed. However, there is one restriction with MERGE statements: you cannot update a column that has been referenced in the ON condition clause. This tip will help you cope with this sort of situation.

For example, let's assume that we have two tables, "category" and "ETL_category":

SQL> desc category
Name    Type
----  ------------
id   Number
description Varchar2(25)
exp_date Date

SQL> desc etl_category
Name    Type
----  ------------
id   Number
catg_name Varchar2(25)
exp_date Date

Then, update the exp_date column of the table "category" with the exp_date column of the "etl_category" table if records exist in "etl_catgeory" for the corresponding ID, otherwise insert a record into the "category" table:

MERGE into category A using etl_category B ON (a.id = b.id)
WHEN MATCHED THEN UPDATE set a.expdate = b.expdate
WHEN NOT MATCHED THEN INSERT (id,description, exp_date) VALUES
(b.id,b.catg_name,b.exp_date);

Now, let's assume that there is a condition on exp_date also; e.g., only update those records if exp_date of "category" < exp_date of "etl_category" for the matching ID's. That may seem simiple, but if you try the following:

MERGE into category A using etl_category B ON (a.id = b.id  AND a.expdate < b.expdate)
WHEN MATCHED THEN UPDATE set a.expdate = b.expdate
WHEN NOT MATCHED THEN INSERT (id,description, exp_date) VALUES
(b.id,b.catg_name,b.exp_date);

You get...

    
ERROR at line 2: ORA-00904: "A"."EXP_DATE": invalid identifier
The reason you see the ORA-00904 is not because of any syntax error; it's because you cannot update a column that has been referenced in the ON condition.

The solution to the problem is to use the SIGN and DECODE functions in the MERGE statement:

MERGE into category A using etl_category B on (a.id=b.id )
WHEN MATCHED THEN UPDATE set a.exp_date = decode ( sign((a.exp_date -
b.exp_date )),-1,b.exp_date,a.exp_date)
WHEN NOT MATCHED THEN INSERT (id,description,exp_date) values
(b.id,b.catg_name,b.exp_date);
The SIGN function returns either -1 (if the supplied number is less than zero), 0 (if the number is zero) or 1 (if the number is greater than zero).


Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle error messages
Why am I receiving Oracle memory allocation errors?
Oracle tutorial library: SearchOracle.com's learning guides
Oracle error 6550 may mean incorrect Oracle export version
Why does the archive log in Oracle give me the ORA-16032 error?
Are we getting the ORA-00382 error because we're at maximum block size?
How do I solve the ORA-00257 error in Oracle?
Solving common Oracle errors guide
Error during RMAN backup
ORA-12560 error with Oracle 10g Instant Client
Unable to view Oracle tables in NetBeans

Oracle database administrator
Understanding SQL string functions
What is the difference between a database engineer, architect and administrator?
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
How can I find statistics on total memory usage and database connections?
Installing multiple Oracle homes
Modifying SYS password in a RAC environment

Oracle and SQL
Oracle tutorial library: SearchOracle.com's learning guides
Can I specify Oracle column order in my database table?
Review: Oracle's 11g R2 database has some good and bad
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
Stored procedures in PL/SQL
PL/SQL functions and triggers in Oracle
Do I need a license for SQL Developer Data Modeler in Oracle?
Using the SQL GROUP BY clause for counting combinations
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Oracle Development Solutions - SQL, J2EE, XML, SOA
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts