在SQL Server中使用JOIN更新表?

我想更新表中的一个列,使其他表上的连接,例如:

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' 

但是这是抱怨:

Msg 170,Level 15,State 1,Line 2
第2行:“a”附近的语法不正确。

这里有什么问题?

您不完全拥有SQL Server专有的UPDATE FROM语法。 也不知道为什么你需要在CommonField上join,并在之后进行过滤。 尝试这个:

 UPDATE t1 SET t1.CalculatedColumn = t2.[Calculated Column] FROM dbo.Table1 AS t1 INNER JOIN dbo.Table2 AS t2 ON t1.CommonField = t2.[Common Field] WHERE t1.BatchNo = '110'; 

如果你正在做一些非常愚蠢的事情 – 比如不断尝试将一列的值设置为另一列的聚合(这违反了避免存储冗余数据的原则),则可以使用CTE(公用表expression式) :

 ;WITH t2 AS ( SELECT [key], CalculatedColumn = SUM(some_column) FROM dbo.table2 GROUP BY [key] ) UPDATE t1 SET t1.CalculatedColumn = t2.CalculatedColumn FROM dbo.table1 AS t1 INNER JOIN t2 ON t1.[key] = t2.[key]; 

这真是愚蠢的原因是,你将不得不重新运行这个整个更新每一次table2任何行更改。 SUM是你可以在运行时总是计算的东西,这样做,永远不用担心结果是陈旧的。

试试像这样:

 begin tran UPDATE a SET a.CalculatedColumn= b.[Calculated Column] FROM table1 a INNER JOIN table2 b ON a.commonfield = b.[common field] WHERE a.BatchNO = '110' commit tran 

(编辑:打字错误!)

Aaron给出的答案是完美的:

 UPDATE a SET a.CalculatedColumn = b.[Calculated Column] FROM Table1 AS a INNER JOIN Table2 AS b ON a.CommonField = b.[Common Field] WHERE a.BatchNo = '110'; 

只是想添加为什么这个问题发生在SQL Server中,当我们尝试在更新表时使用表的别名,下面提到的语法总是会给出错误:

 update tableName t set t.name = 'books new' where t.id = 1 

大小写可以是任何如果您正在更新单个表或使用连接更新。

尽pipe上面的查询在PL / SQL中可以正常工作,但在SQL Server中却不能。

在SQL Server中使用表别名时更正表的正确方法是:

 update t set t.name = 'books new' from tableName t where t.id = 1 

希望这会帮助大家为什么错误来到这里。

似乎SQL Server 2012也可以处理Teradata的旧更新语法:

 UPDATE a SET a.CalculatedColumn= b.[Calculated Column] FROM table1 a, table2 b WHERE b.[common field]= a.commonfield AND a.BatchNO = '110' 

如果我没有记错的话,当我尝试类似的查询时,2008 R2发生了错误。

 MERGE table1 T USING table2 S ON T.CommonField = S."Common Field" AND T.BatchNo = '110' WHEN MATCHED THEN UPDATE SET CalculatedColumn = S."Calculated Column"; 

我发现将UPDATE转换成SELECT可以在更新之前获得我想要更新的行作为testing。 如果我可以select我想要的确切的行,我可以更新我想要更新的那些行。

 DECLARE @expense_report_id AS INT SET @expense_report_id = 1027 --UPDATE expense_report_detail_distribution --SET service_bill_id = 9 SELECT * FROM expense_report_detail_distribution erdd INNER JOIN expense_report_detail erd INNER JOIN expense_report er ON er.expense_report_id = erd.expense_report_id ON erdd.expense_report_detail_id = erd.expense_report_detail_id WHERE er.expense_report_id = @expense_report_id 
  UPDATE mytable SET myfield = CASE other_field WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' END From mytable Join otherTable on otherTable.id = mytable.id Where othertable.somecolumn = '1234' 

更多的select在这里: http : //www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/

另一种方法是使用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

尝试:

 UPDATE table1 SET CalculatedColumn = ( SELECT [Calculated Column] FROM table2 WHERE table1.commonfield = [common field]) WHERE BatchNO = '110'