merge文

(12R1)

drop table tab1 purge;
drop table tab2 purge;

create table tab1(col1 int,col2 int);
create unique index ind1 on tab1(col1);

create table tab2(col1 int,col2 int);
create unique index ind2 on tab2(col1);

insert into tab1 values(1,1);
insert into tab2 values(1,21);
insert into tab2 values(2,22);

commit;

select * from tab1;
select * from tab2;

merge into tab1 t1
using tab2 t2
on ( t1.col1 = t2.col1 )
when matched then
update set
t1.col2 = t2.col2
when not matched then
insert (t1.col1,t1.col2)
values (t2.col1,t2.col2)
;

select * from tab1;
select * from tab2;

 

https://qiita.com/yadok/items/fa72e8ae1d97033be218

(8.0.29)

drop table tab1;
drop table tab2;

create table tab1(col1 int,col2 int);
create unique index ind1 on tab1(col1);

create table tab2(col1 int,col2 int);
create unique index ind2 on tab2(col1);

insert into tab1 values(1,1);
insert into tab2 values(1,21);
insert into tab2 values(2,22);


select * from tab1;
select * from tab2;


insert into tab1 (col1,col2) select col1,col2 from tab2 on duplicate key update tab1.col2 = tab2.col2
;

select * from tab1;
select * from tab2;

 

REPLACE は、テーブルに PRIMARY KEY または UNIQUE インデックスがある場合にのみ意味を持ちます。

replace into tab1 (col1,col2) select col1,col2 from tab2
;

 

 

https://dev.classmethod.jp/server-side/db/postgresql-9-5-new-function-upsert-use/


(9.6)

drop table tab1;
drop table tab2;

create table tab1(col1 int,col2 int);
create unique index ind1 on tab1(col1);

create table tab2(col1 int,col2 int);
create unique index ind2 on tab2(col1);

insert into tab1 values(1,1);
insert into tab2 values(1,21);
insert into tab2 values(2,22);


select * from tab1;
select * from tab2;

insert into tab1 (col1,col2) select col1,col2 from tab2 on conflict(col1) do update set col2 = excluded.col2
;


select * from tab1;
select * from tab2;


※挿入されようとしていた行には、特別なexcludedテーブルを使ってアクセスする

(15)

drop table tab1 ;
drop table tab2 ;

create table tab1(col1 int,col2 int);

create table tab2(col1 int,col2 int);

insert into tab1 values(1,1);
insert into tab2 values(1,21);
insert into tab2 values(2,22);


select * from tab1;
select * from tab2;

merge into tab1 t1
using tab2 t2
on  t1.col1 = t2.col1 
when matched then
update set
col2 = t2.col2
when not matched then
insert (col1,col2) values (t2.col1,t2.col2)
;


※結合条件にユニークキーは不要
※マージ先のテーブルカラムをテーブル別名で修飾するとエラーとなる


select * from tab1;
select * from tab2;

 
 

https://blog.beatdjam.com/entry/2015/03/06/010808

(2014)

drop table tab1;
drop table tab2;

create table tab1(col1 int,col2 int);
create unique index ind1 on tab1(col1);

create table tab2(col1 int,col2 int);
create unique index ind2 on tab2(col1);

insert into tab1 values(1,1);
insert into tab2 values(1,21);
insert into tab2 values(2,22);


select * from tab1;
select * from tab2;

merge into tab1 t1
using tab2 t2
on ( t1.col1 = t2.col1 )
when matched then
update set
col2 = t2.col2
when not matched then
insert (col1,col2)
values (t2.col1,t2.col2)
;

select * from tab1;
select * from tab2;


※t1で修飾するとエラーとなる