合并-有条件的“何时匹配然后更新" [英] MERGE - conditional "WHEN MATCHED THEN UPDATE"

查看:56
本文介绍了合并-有条件的“何时匹配然后更新"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图中的突出显示了我要实现的逻辑.我意识到语法不正确.

The highlights in the image below shows the logic I want to implement. I realize the syntax is incorrect.

是否只有在目标表中其列之一的值为空且源表中的对应值不为空的情况下,才有条件地更新MERGE语句中的记录?

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?

您如何建议重写此内容?

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

推荐答案

您可能可以使用当匹配为And(例如[创建日期]不为空且t.[创建日期]为空)时,然后更新... .

这篇关于合并-有条件的“何时匹配然后更新"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆