我是MERGE的新手 - 我有一个有效的MERGE声明:
merge customers with (holdlock) as c
using (select @id as id) as new_id
on c.contactid = new_id.id
when matched then
update set ContactID = 1, firstname = CAST(SYSDATETIME() AS VARCHAR(100))
when not matched then
insert(ContactID) values(1), firstName = 'b';
(SQL并不重要 - 这是出于测试目的)
我的问题是 - 我怎么能返回一些内容,表明是否发生了插入或更新?
答案 0 :(得分:4)
您可以在合并语句中使用OUTPUT子句和关键字$ action。
像......那样的东西。
merge customers with (holdlock) as c
using (select @id as id) as new_id
on c.contactid = new_id.id
when matched then
update set ContactID = 1, firstname = CAST(SYSDATETIME() AS VARCHAR(100))
when not matched then
insert(ContactID) values(1), firstName = 'b'
OUTPUT $action, inserted.*, deleted.*;
上面的输出子句将返回第一列中的操作,如update,insert或delete以及inserted和deleted表中的值。