VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

MERGE INTO

1. 准备测试数据
CREATE TABLE target (id integer, balance integer);
CREATE TABLE source (id integer, delta integer);
INSERT INTO target VALUES (1, 10);
INSERT INTO target VALUES (2, 20);
INSERT INTO target VALUES (3, 30);
INSERT INTO source VALUES (3, 30);
INSERT INTO source VALUES (4, 40);
INSERT INTO source VALUES (5, 50);
2. 测试merge into
MERGE INTO target AS t
USING source AS s
ON t.id = s.id
WHEN MATCHED THEN
	update set t.balance=s.delta
WHEN NOT MATCHED THEN
	INSERT VALUES (s.id, s.delta);
3. 检查
select * from target;