更新 LEFT JOIN 错误

UPDATE on LEFT JOIN error

我有一个 SQL 查询一直给我一个错误。

我已经尝试了多种编写查询的方法,但都没有修复它。

我有两个表(table1 和 table2),其中包含重复的列 orgcodeold 和 orgcode。 table1.orgcode 为空,但 table2.orgcode 已填充。

我正在尝试用 table2.orgcode 填充 table1.orgcode,其中 table1.orgcode旧=table2.orgcode旧.

错误

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.

查询

UPDATE table1 AS t1 
LEFT JOIN table2 AS t2 
ON t2.orgcodeold = t1.orgcodeold 
SET t1.orgcode = t2.orgcode 
WHERE t1.orgcodeold = t2.orgcodeold

请帮忙。

好吧,你几乎整个语法都错了。应该是:

UPDATE t1
SET t1.orgcode = t2.orgcode 
FROM table1 AS t1 
INNER JOIN table2 AS t2 
    ON t2.orgcodeold = t1.orgcodeold;

这应该有效:

UPDATE t1
SET t1.orgcode = t2.orgcode 
from table1 AS t1
LEFT JOIN table2 AS t2 
ON t2.orgcodeold = t1.orgcodeold