我试图在更新声明中加入三个表,但到目前为止我都没有成功.我知道这个查询适用于连接两个表:
update table 1 set x = X * Y from table 1 as t1 join table 2 as t2 on t1.column1 = t2.column1
但是,在我的情况下,我需要加入三个表,所以:
update table 1 set x = X * Y from table 1 as t1 join table 2 as t2 join table3 as t3 on t1.column1 = t2.column1 and t2.cloumn2 = t3.column1
不管用.我也尝试了以下查询:
update table 1 set x = X * Y from table 1,table 2,table 3 where column1 = column2 and column2= column3
有谁知道实现这个的方法?
解决方法
你绝对不想使用表,表,表语法;
here’s why.对于您的中间代码示例,连接语法遵循与UPDATE大致相同的SELECT规则. JOIN t2 JOIN t3 ON …无效,但JOIN t2 ON … JOIN t3 ON有效.
所以这是我的建议,尽管应该更新以完全符合y的来源:
UPDATE t1 SET x = x * y -- should either be t2.y or t3.y,not just y FROM dbo.table1 AS t1 INNER JOIN table2 AS t2 ON t1.column1 = t2.column1 INNER JOIN table3 AS t3 ON t2.column2 = t3.column1;