Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, sravani.

Asked: March 01, 2017 - 8:45 pm UTC

Last updated: March 02, 2017 - 4:56 pm UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

Hi,

I am working on Merge statement. I know Merge is used to update/insert/delete the data. In my program,for new record do insert and existing record,do nothing.But when using merge statement 'ON' clause we are checking the data with the source table and target table.My table doesn't have unique values so I need to check every column and insert new record if any of the column is not matched. So Question is do I need to check every column or is there anyway we can check whole row data?

Thanks,
Sravani

and Connor said...

Sorry, you'll need to check each column. To take care of nulls, you can use a DECODE, eg

SQL> merge into emp e
  2  using ( select * from scott.emp ) m
  3  on (   decode(m.empno,e.empno,1,0) = 1
  4    and decode(m.ename,e.ename,1,0) = 1
  5    and decode(m.deptno,e.deptno,1,0) = 1
  6    )
  7  when matched then
  8  update set sal = m.sal;

14 rows merged.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Rowid

Gh.., March 02, 2017 - 6:10 am UTC

Merge /*+ rowid (tgt) */ t1 tgt
Using ( select t1.rowid as rid , t2.col ..from t1 ...join t2.....) src
On tgt.rowid = src.rid
When matched then...
.....

So you only want to insert?

Stew Ashton, March 02, 2017 - 9:33 am UTC

If so, why not just insert?
insert into emp
select * from scott.emp
minus
select * from emp;

Chris Saxon
March 02, 2017 - 4:56 pm UTC

Yep, that could work too.

sravani addanki, March 02, 2017 - 5:10 pm UTC