下图中的亮点显示了我想要实现的逻辑。我意识到语法不正确。
有没有办法有条件地更新 MERGE 语句中的记录,仅当目标表中某一列的值为 NULL,并且源表中相应的值不为 null 时?
你建议如何重写这个?
MERGE dbo.input_311 AS [t]
USING dbo.input_311_staging AS [s]
ON ([t].[unique key] = [s].[unique key])
WHEN NOT MATCHED BY TARGET
THEN INSERT(t.[Created Date]) VALUES(s.[Created Date])
WHEN MATCHED
THEN UPDATE SET(t.[Created Date] = s.[Created Date]
WHERE s.[Created Date] IS NOT NULL
AND t.[Created Date] IS NULL)
OUTPUT deleted.*, $action, inserted.*;
GO
最佳答案
您也许可以使用When Matched And (s.[Created Date] Is Not Null And t.[Created Date] Is Null) then Update ...
。
关于sql - 合并 - 有条件 "WHEN MATCHED THEN UPDATE",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13924238/