20

下图中的高亮部分显示了我想要实现的逻辑。我意识到语法不正确。

只有当目标表中的一个列的值为 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
4

1 回答 1

50

您也许可以使用When Matched And (s.[Created Date] Is Not Null And t.[Created Date] Is Null) Then Update ....

于 2012-12-17T23:59:51.563 回答