MERGE table, do nothing when matched MERGE table, do nothing when matched oracle oracle

MERGE table, do nothing when matched


For your case, no need to use the part:

WHEN MATCHED THEN UPDATE ...

( using WHEN MATCHED THEN UPDATE SET a.id = a.id is accepted(Oracle doesn't hurl) but has no impact, so, such a usage is redundant, because you don't want to change anything for the matching case. )

If you wanted to change, then add

WHEN MATCHED THEN UPDATE SET a.id = b.id

before WHEN NOT MATCHED THEN INSERT...

( e.g.Oracle supports WHEN MATCHED THEN UPDATE syntax. Refer theDemo below )

Go on with the following for the current case :

SQL> CREATE TABLE domains(                            id          INT,                            name        VARCHAR2(50),                            code        VARCHAR2(50),                            description VARCHAR2(50)                         );SQL> INSERT INTO domains VALUES(1,'Domain A','D.A.','This is Domain A');SQL> MERGE INTO domains A USING      (SELECT 2 id, 'Domain A' name, 'D.A.' code, 'This is Domain A' description         FROM domains) b          ON ( a.name = b.name )        WHEN NOT MATCHED THEN INSERT( a.id, a.name, a.code, a.description )                               VALUES( b.id, b.name, b.code, b.description );SQL> SELECT * FROM domains;ID  NAME        CODE    DESCRIPTION--  --------   -----  ----------------1   Domain A    D.A.  This is Domain ASQL> DELETE domains;SQL> INSERT INTO domains VALUES(1,'Domain A','D.A.','This is Domain A');-- we're deleting and inserting the same row againSQL> MERGE INTO domains A USING        (SELECT 2 id, 'Domain B' name, 'D.B.' code, 'This is Domain B' description     FROM domains) b      ON ( a.name = b.name )    WHEN NOT MATCHED THEN INSERT( a.id, a.name, a.code, a.description )                           VALUES( b.id, b.name, b.code, b.description );SQL> SELECT * FROM domains;ID  NAME        CODE    DESCRIPTION--  --------   -----  ----------------1   Domain A    D.A.  This is Domain A2   Domain B    D.B.  This is Domain B

Demo


Oracle SQL syntax supports not having any when matched then update clause.

drop table ft purge;create table ft (c1 number, c2 varchar2(10));drop table ld purge;create table ld (c1 number, c2 varchar2(10));insert into ft values (1,'a');insert into ld values (1,'b');insert into ld values (2,'c');commit;merge into ft using ldon (ft.c1 = ld.c1) when not matched theninsert (c1,c2) values (ld.c1,ld.c2);select * from ft;C1  C2--- ---1   a2   c2 rows selected.


Why not use
merge into A
using B
on ...
when not matched then
...

which I've verified is valid ...