将记录插入表中,当匹配时和不匹配时,在Merge语句中记录

时间:2019-03-15 04:52:44

标签: sql sql-server

我希望在Merge语句SQL Server中匹配时和不匹配时执行相同的插入操作。

MERGE TargetTable f 
USING ( SELECT * FROM SourceTable ) q ON q.PKID = f.PKID 
WHEN MATCHED AND ( q.a != f.a OR q.b != f.b OR q.c != f.c ) THEN 
INSERT (a,b,c,CreatedDate) VALUES (q.a,q.b,q.c,Getdate()); 
WHEN NOT MATCHED BY TARGET THEN 
INSERT (a,b,c,CreatedDate) VALUES (q.a,q.b,q.c,Getdate());

0 个答案:

没有答案