在SQL Server 2008中的MERGE
语句中存在WHEN NOT MATCHED BY SOURCE
和WHEN NOT MATCHED BY TARGET
,示例取自Inserting, Updating, and Deleting Data by Using MERGE
-- MERGE statement with join conditions that produce unexpected results.
USE tempdb;
GO
BEGIN TRAN;
MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID AND T.EmployeeName LIKE 'S%'
AND S.EmployeeName LIKE 'S%' )
WHEN NOT MATCHED BY TARGET
THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED
THEN UPDATE SET T.EmployeeName = S.EmployeeName
WHEN NOT MATCHED BY SOURCE
THEN DELETE
OUTPUT $action, Inserted.*, Deleted.*;
ROLLBACK TRAN;
GO
DB2中有这样的东西吗?例如,当我执行MERGE
并且表中的任何记录数据存在但不存在于查询中时,NOT MATCH BY SOURCE
它将从表中删除数据。
答案 0 :(得分:1)
我目前正在寻找类似问题的答案,据我所知,DB2不支持它。 根据这里的文件(http://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0010873.html):
NOT MATCHED表示要对其中的行执行的操作 ON搜索条件为false或未知。只有INSERT或 可以在那之后指定signal-statement。
因此,如果执行NOT MATCHED条件,则不能使用DELETE命令。 您可能需要执行单独的查询以删除与合并条件不匹配的数据。