SQL MERGE - 有条件的“WHEN MATCHED THEN UPDATE”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13924238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MERGE - conditional "WHEN MATCHED THEN UPDATE"
提问by sion_corn
The highlights in the image below shows the logic I want to implement. I realize the syntax is incorrect.
下图中的亮点显示了我想要实现的逻辑。我意识到语法不正确。
Is there a way to conditionally update a record in a MERGE statement only if it the value of one of its columns in the target table is NULL, and the corresponding value in the source table is not null?
有没有办法有条件地更新 MERGE 语句中的记录,前提是它在目标表中的一个列的值为 NULL,并且源表中的相应值不为空?
How would you suggest re-writing this?
你怎么建议重写这个?
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
回答by Laurence
You might be able to use When Matched And (s.[Created Date] Is Not Null And t.[Created Date] Is Null) Then Update ...
.
您也许可以使用When Matched And (s.[Created Date] Is Not Null And t.[Created Date] Is Null) Then Update ...
.