Considering the following tables:
Users
----------------------
Id | 1 | 2
Name| John| Jack
Cars
----------------------
Id | 14 | 26
Name| Mercedes| BMW
Import
-----------------------
Id | 12 | 34
UserName | John | Daniel
UserId | NULL | NULL
CarName | BMW | Mercedes
CarId | NULL | NULL
SomeOtherId| 45 | 45
I basically want to find UserId and CarId in the other tables, If they are not find they remain NULL,
I tried something like
UPDATE Import i
SET UserId = ( SELECT Id FROM Users WHERE Name=i.UserName ),
CarId = ( SELECT Id FROM Cars WHERE Name= i.CarName )
WHERE SomeOtherId=45;
In this case i am getting an incorrect syntax.
How can i fix it?
Is it a way to use MERGE in this case? (merging more than 2 tables and using a WHERE clause)?
Thanks.
You might use
fromclause in update itself: