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?
You don’t quite have SQL Server’s proprietary
UPDATE FROMsyntax down. Also not sure why you needed to join on theCommonFieldand also filter on it afterward. Try this: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:
The reason this is silly, is that you’re going to have to re-run this entire update every single time any row in
table2changes. ASUMis something you can always calculate at runtime and, in doing so, never have to worry that the result is stale.