TSQL MERGE命令似乎实际上没有合并

时间:2013-01-11 21:49:43

标签: tsql merge

我正在研究代码here

当我从那里剪切粘贴以下代码时:

- 正确指定了连接条件的MERGE语句。

USE tempdb;
GO
BEGIN TRAN;
MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID) 
WHEN NOT MATCHED BY TARGET AND S.EmployeeName LIKE 'S%' 
    THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED 
    THEN UPDATE SET T.EmployeeName = S.EmployeeName
WHEN NOT MATCHED BY SOURCE AND T.EmployeeName LIKE 'S%'
    THEN DELETE 
OUTPUT $action, inserted.*, deleted.*;
ROLLBACK TRAN;
GO 

它产生他们说的输出,但它实际上并没有改变TARGET表。如何让它实际更改TARGET表的结果?有某种转换吗?

1 个答案:

答案 0 :(得分:4)

ROLLBACK TRAN;替换为COMMIT TRAN;

目标表正在被更改,因此您看到了正确的OUTPUT,但是您通过回滚事务在下一个语句中撤消它。