首页 > 代码库 > 新学习的命令

新学习的命令

1.Merge

create table sourceTable(id int ,val varchar(20))
create table targetTable (id int,val varchar(20))
insert into targetTable values(1,‘a‘),(2,‘b‘),(3,‘c‘)
insert into sourceTable values(3,‘c‘),(4,‘d‘),(4,‘d‘)

----

MERGE INTO TargetTable AS T
USING SourceTable AS S
ON T.id=S.ID
WHEN MATCHED
THEN
UPDATE SET t.Val=s.val
WHEN NOT MATCHED
THEN INSERT VALUES(s.id,s.val)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;

新学习的命令