create table MERGE_DELETE_TEST1 (I int primary key, J int, C varchar2(20))
create table MERGE_DELETE_TEST2 (I int primary key, J int, C varchar2(20))
insert into MERGE_DELETE_TEST1 values (1, 1, 'one')
insert into MERGE_DELETE_TEST1 values (2, 1, 'two')
insert into MERGE_DELETE_TEST1 values (3, 1, 'three')
insert into MERGE_DELETE_TEST1 values (4, 1, 'four')
insert into MERGE_DELETE_TEST2 values (2, 1, 'two')
insert into MERGE_DELETE_TEST2 values (3, 3, 'x')
insert into MERGE_DELETE_TEST2 values (4, null, 'dirigible')
insert into MERGE_DELETE_TEST2 values (99, 2, 'bobs your uncle')
commit
select t1.I, t1.J, t1.C, t2.I as X, t2.J as Y, t2.C as Z
from MERGE_DELETE_TEST1 t1
full join MERGE_DELETE_TEST2 t2
on t2.I = t1.I
order by t1.I, X
merge into MERGE_DELETE_TEST2 tgt
using (
select t1.I, t1.J, t1.C, t2.I as X, t2.J as Y, t2.C as Z
from MERGE_DELETE_TEST1 t1
full join MERGE_DELETE_TEST2 t2
on t2.I = t1.I
) src
on (
tgt.I = src.I
or src.I is null
)
when matched then
update set tgt.J = src.J, tgt.C = src.C
where tgt.I = src.I
delete where src.I is null and tgt.I is not null
when not matched then
insert (I, J, C)
values (src.I, src.J, src.C)
select t1.I, t1.J, t1.C, t2.I as X, t2.J as Y, t2.C as Z
from MERGE_DELETE_TEST1 t1
full join MERGE_DELETE_TEST2 t2
on t2.I = t1.I
order by t1.I, X
select * from MERGE_DELETE_TEST1 order by I
select * from MERGE_DELETE_TEST2 order by I
commit
drop table MERGE_DELETE_TEST1
drop table MERGE_DELETE_TEST2