我有以下SQL查询,它完美运行并显示结果
WITH tbl1 AS
(
SELECT
tbl2.[PERSON_NAME] AS tbl2_name, *
FROM [tableToUpdate] tbl2
)
SELECT
tbl1.[PERSON_NAME], tbl3.[name]
FROM
tbl1
LEFT JOIN
[tableFirstJoin] tbl3 ON tbl3.[name] = tbl2_name
INNER JOIN
[tableSecondJoin] tbl4 ON tbl4.[matchColumn] = tbl3.[matchColumn]
WHERE
tbl1.[PERSON_NAME] is not null
但我现在想要添加一个类似于此
的更新语句UPDATE [tableToUpdate]
SET [column1] = tbl4.[column1], [column2] = tbl4.[column2]
FROM [tableToUpdate] tbl5
所以我想更新with语句中同一个表中的列,但是要使用外部的值。
有谁知道如何编写查询来执行此更新?
答案 0 :(得分:0)
如果您想通过cte进行更新,语法为:
WITH cte AS (SELECT field FROM tbl)
UPDATE t1
SET t1.field = t2.field
FROM cte t1
INNER JOIN othertable t2 ON t1.field = t2.otherfield