如何使用另一个表列更新 SQL Server 列

问题描述 投票:0回答:1

我有一张看起来像这样的桌子

表1

Source                 OriginalCreatedDate
[email protected]        2022-12-24 06:01:25.000
[email protected]        

表2

Source                 CreatedDate
[email protected]        2022-12-24 06:01:25.000
[email protected]        2023-08-07 02:01:25.000

我只是想知道如何使用表 2 CreatedDate 列更新表 1 OriginalCreatedDate 列。

我相信我需要使用源列来比较两个表,但我只是想知道如何更新仅具有 null 或 '' 值的 Table1 行。我想忽略已经具有 OriginalCreatedDate 值的行。

       Update Table1
        Set Original_CreatedDate = ISNULL (( SELECT Top 1 Table2.CreatedDate FROM Table2
        WHERE Table2.Source = Table1.Source), Table1.Original_CreatedDate)
        GO

任何帮助或建议将不胜感激

sql sql-server sql-server-2012 multiple-columns
1个回答
0
投票
UPDATE t1
SET
  OriginalCreatedDate = t2.CreatedDate
FROM
  Table1 AS t1
  JOIN Table2 AS t2 ON t1.Source = t2.Source
WHERE
  (t1.OriginalCreatedDate IS NULL) OR (t1.OriginalCreatedDate = '')
© www.soinside.com 2019 - 2024. All rights reserved.