-2

I'm using Microsoft SQL Server and when writing this code I am getting an incorrect syntax error for the "Inner" and also "=" within the SET line. I've looked into this and think everything is set up correctly, can someone point out why if possible?

USE [db1]

GO

UPDATE a.[table1] 
INNER JOIN a.[table1] = a.[table2]
ON a.[table1].[field1] = a.[table2].[field1]
SET [field2] = a.[table2].[field2]
WHERE [ID] IN (SELECT #TempTable.[ID] FROM #TempTable)
AJS
  • 3
  • 1
  • 1
    I doubt you searched very long. First result: [SQL update query using joins](http://stackoverflow.com/questions/982919/sql-update-query-using-joins) – HoneyBadger Feb 08 '17 at 12:55

4 Answers4

2

Well, I have absolutely no idea where you got the syntax for an UPDATE with a JOIN from, but it's pretty messed up. This should be the right code:

UPDATE t1
SET t1.field2 = t2.field2
FROM dbo.table1 t1
INNER JOIN dbo.table2 t2
    ON t1.field1 = t2.field2
WHERE [ID] IN (SELECT [ID] FROM #TempTable);
Lamak
  • 69,480
  • 12
  • 108
  • 116
0
UPDATE  a
SET [field2] = a.[table2].[field2]
from  a.[table1] 
INNER JOIN a.[table1] = a.[table2]
ON a.[table1].[field1] = a.[table2].[field1]
WHERE [ID] IN (SELECT #TempTable.[ID] FROM #TempTable)

See if it is working

Ayush Kalia
  • 31
  • 1
  • 7
0

try following

UPDATE a.[table1] 
SET [field2] = a.[table2].[field2]
From a.[table1]
INNER JOIN a.[table2]
ON a.[table1].[field1] = a.[table2].[field1]
WHERE [ID] IN (SELECT #TempTable.[ID] FROM #TempTable)
jjj
  • 1,136
  • 3
  • 18
  • 28
0

Update using inner join

UPDATE T1
    SET T1.[field2] = T2.[field2]
    FROM a.[table1] AS T1 
    INNER JOIN a.[table2] AS T2
    ON T1.[field1] = T2.[field1]
PowerStar
  • 893
  • 5
  • 15