LEFT JOIN错误更新

时间:2015-02-03 16:03:29

标签: sql sql-server sql-update left-join

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

我尝试了多种编写查询的方法,但我没有运气来修复它。

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

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

错误

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

QUERY

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

请帮忙。

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

这应该有效:

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