Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a table using JOIN in SQL Server?

I want to update a column in a table making a join on other table e.g.:

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' 

But it is complaining :

Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'a'.

What is wrong here?

like image 656
Manjot Avatar asked Oct 21 '09 22:10

Manjot


People also ask

How do you update a table with a join in SQL?

SQL Server UPDATE JOIN syntaxFirst, specify the name of the table (t1) that you want to update in the UPDATE clause. Next, specify the new value for each column of the updated table. Then, again specify the table from which you want to update in the FROM clause.

Can we use update with joins?

The most easiest and common way is to use join clause in the update statement and use multiple tables in the update statement. Here we can see that using join clause in update statement. We have merged two tables by the use of join clause.

Can we join tables in update query?

It is possible to join two or more tables in an UPDATE query.

How can I update existing table in SQL Server?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.


2 Answers

Try it like this:

    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' 
like image 37
RBarryYoung Avatar answered Oct 01 '22 13:10

RBarryYoung


You don't quite have SQL Server's proprietary UPDATE FROM syntax down. Also not sure why you needed to join on the CommonField and also filter on it afterward. Try this:

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'; 

If you're doing something silly - like constantly trying to set the value of one column to the aggregate of another column (which violates the principle of avoiding storing redundant data), you can use a CTE (common table expression) - see here and here for more details:

;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]; 

The reason this is silly, is that you're going to have to re-run this entire update every single time any row in table2 changes. A SUM is something you can always calculate at runtime and, in doing so, never have to worry that the result is stale.

like image 170
Aaron Bertrand Avatar answered Oct 01 '22 13:10

Aaron Bertrand