sql - 合并 - 有条件 "WHEN MATCHED THEN UPDATE"

标签 sql sql-server tsql merge data-warehouse

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

有没有办法有条件地更新 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/

相关文章:

c# - 为 P1 SQL 数据库创建 Azure ClusteredColumnStoreIndex 的 C# SMO 方法是什么

tsql - 在删除触发器中更新表以删除多行

c# - 为什么参数比 where 子句中的文字值慢?

MySQL 使用单独的 SELECT 查询从多个表插入表

sql - 甲骨文 11g : only return a value if row is first or last in group

mysql - 如何使用Max聚合函数获取行的值?

sql-server - 当sql server 2005中的try catch捕获错误时如何防止事务回滚

javascript - 对使用 SQL Server 数据库的 ASP 站点的攻击

sql - 如何根据最大时间间隔进行选择

sql - TSQL : Max date but include other variables