Geeks With Blogs
Will Smith The Blog in Black

I encountered a problem with Oracle's MERGE DML yesterday.  What I was trying to do is use the "Upsert" feature of the merge, except with no distinct source.  All of the searching I did for the MERGE showed examples of how to merge data from a source into a target.  In my case, I thought the source and the target were the same.

This is what I initially tried.  It didn't work.  It turns out that if the source (s) returns an empty set, then the merge is effectively a no-op

   1: merge into MY_TARGET t
   2: using (select *
   3:          from MY_TARGET
   4:         where COL1 = :p1
   5:           and COL2 = :p2) s
   6:   on (t.COL1 = s.COL1 
   7:   and t.COL2 = s.COL2)
   8: when matched then
   9:   update set t.COL3 = :p3
  10: when not matched then
  11:   insert (COL1, COL2, COL3) 
  12:   values (:p1, :p2, :p3)

I thought for a while to figure out how I could get the source to always return something.  Finally it occurred to me that I could use DUAL.

   1: merge into MY_TARGET t
   2: using (select 1 from DUAL) s
   3:   on (t.COL1 = :p1 
   4:   and t.COL2 = :p2)
   5: when matched then
   6:   update set t.COL3 = :p3
   7: when not matched then
   8:   insert (COL1, COL2, COL3) 
   9:   values (:p1, :p2, :p3)

I hope this helps someone else.  At the very least it will be here the next time I need it.

Tags: , ,
Posted on Wednesday, March 5, 2008 12:53 AM Oracle , Database | Back to top


Comments on this post: Oracle Merge To Self

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks.. you helped me alot
Left by tytang on May 06, 2008 8:53 PM

# re: Oracle Merge To Self
Requesting Gravatar...
awesome!!!! i has similar issue, cracked my head for 3 hours, search the web, nothing!!
bam! finally saw your post, tried it, worked like a charm...

Thanks a million.

sam
Left by sam on Jun 27, 2008 1:05 AM

# re: Oracle Merge To Self
Requesting Gravatar...
I'm glad to have helped you guys!
Left by Will Smith on Jun 28, 2008 7:58 PM

# re: Oracle Merge To Self
Requesting Gravatar...
The case of empty source(s) did not mentioned on the web. I got the same error with you. Your way to do it, is great.

Moreover, when your target and source is the same table, you can just use " using (t.COL1 = :p1 and t.COL2 = :p2) " syntax.

Thanks for sharing your best practices ...
Left by Mehmet Özakan on Jan 05, 2009 6:05 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thank You. this is exactly what I needed and it was a BIG help to me.
Left by Dee on Jan 19, 2009 8:35 AM

# re: Oracle Merge To Self
Requesting Gravatar...
I'm trying to do something similar with bound variables and not having much luck.

MERGE INTO myTarget h
USING (SELECT 1 FROM dual) s
ON (h.H3=:h3)
WHEN MATCHED THEN UPDATE
SET h.H4=:h4, h.H5=:h5
WHEN NOT MATCHED THEN INSERT (h.h3, h.h4, h.h5)
VALUES (:h3,:h4,:h5);

Thoughts?
Left by gcuevas on Aug 10, 2009 8:50 PM

# re: Oracle Merge To Self
Requesting Gravatar...
@gcuevas

What kind of error are you getting?

I can't see any problems with the syntax.
Left by Will on Aug 20, 2009 10:26 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thank you very much for this post :)
Left by summerian on Sep 29, 2009 11:08 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks - I was stuck with this problem and your article helped me out .. thanks for sharing this.
Left by Mahim on Sep 30, 2009 3:23 PM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks. Just what I was looking for!
Left by aditya on Oct 12, 2009 5:35 PM

# re: Oracle Merge To Self
Requesting Gravatar...
GREAT WORK!!! Oracle documentation sucks big time. I could not find anything like this on the Oracle site.

Thank you for posting!!

Regards,
Patrick
MTR Investors Group
www.MTRIG.com
Left by Patrick on Oct 23, 2009 9:47 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thank you so much for this solution, I've browsed half the internet and still did not find the solution.
Left by Erik on Feb 22, 2010 7:03 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks !
You helped me out...
Left by Sam on Apr 29, 2010 4:11 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks a lot. I was totally pissed off trying to find solution to this problem. Thanks a lot again..
Left by Inderpal Singh on Jul 14, 2010 11:08 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks a lot!! I was close to collapse :)
Left by AlmostDesperated on Oct 05, 2010 9:56 AM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks, very useful!
Left by Cornal on Nov 19, 2010 1:35 PM

# re: Oracle Merge To Self
Requesting Gravatar...
you could just wrote
USING MY_TARGET t
Left by Nagh on Jan 11, 2011 8:14 AM

# re: Oracle Merge To Self
Requesting Gravatar...
thankss... work perfectly.
use src and target on the same table would render ORA-30926
Left by hayata on Mar 27, 2011 12:35 PM

# re: Oracle Merge To Self
Requesting Gravatar...
Thanks a lot, this is very helpful
Left by mohamed on May 17, 2011 8:56 AM

# re: Oracle Merge To Self
Requesting Gravatar...
This should work too,

merge into MY_TARGET t
using DUAL
on (t.COL1 = :p1 and
t.COL2 = :p2)
when matched then
update set t.COL3 = :p3
when not matched then
insert (COL1, COL2, COL3)
values (:p1, :p2, :p3)
Left by Sid on Jul 30, 2011 11:17 AM

# re: Oracle Merge To Self
Requesting Gravatar...
It's a bad idea to use 'using DUAL' instead of 'using (select1 from dual)' concerning the execution performance
Left by Evan on Feb 05, 2012 11:34 PM

# re: Oracle Merge To Self
Requesting Gravatar...
Awesome, worked perfectly for what I needed
Left by Tony on Mar 15, 2012 4:25 PM

# re: Oracle Merge To Self
Requesting Gravatar...
Good job!
Left by dc on Mar 22, 2012 4:59 PM

# re: Oracle Merge To Self
Requesting Gravatar...
The self merge query helped me to solve my issue very quickly. Thank you so much.
Left by Sathya on Apr 16, 2012 3:41 PM

Your comment:
 (will show your gravatar)
 


Copyright © Will Smith | Powered by: GeeksWithBlogs.net | Join free