下图中的高亮部分显示了我想要实现的逻辑。我意识到语法不正确。
只有当目标表中的一个列的值为 NULL 并且源表中的相应值不为空时,有没有办法有条件地更新 MERGE 语句中的记录?
你会如何建议重写这个?
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