我想在一个表中更新一个列,在其他表上进行连接,例如:
UPDATE table1 a
INNER JOIN table2 b ON a.commonfield = b.[common field]
SET a.CalculatedColumn= b.[Calculated Column]
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
但它在抱怨:
警报170,15层,状态1,2号线
第2行:'a'附近的语法错误。
这里出了什么问题?
另一种方法是使用MERGE
;WITH cteTable1(CalculatedColumn, CommonField)
AS
(
select CalculatedColumn, CommonField from Table1 Where BatchNo = '110'
)
MERGE cteTable1 AS target
USING (select "Calculated Column", "Common Field" FROM dbo.Table2) AS source ("Calculated Column", "Common Field")
ON (target.CommonField = source."Common Field")
WHEN MATCHED THEN
UPDATE SET target.CalculatedColumn = source."Calculated Column";
-Merge是SQL标准的一部分
-我也很确定内部连接更新是不确定的。
类似的问题,答案是这样说的
http://ask.sqlservercentral.com/questions/19089/updating-two-tables-using-single-query.html