Wednesday, March 17, 2010

update with inner join in sql server

You cannot update 2 tables at once (you need 2 statements to do that, one for each statement), but you can update with inner joins. The trick is to use the full table name that you want to update, using the keyword "from" before the "where" but after the "set"


E.g.

update table1 set table1.a = x.a, table1.b = ...
from ...... inner join table99 x on x.id = .... and ...
inner join table1 on table1.id = .........
where x.f in (select f ....) and ...

Just mind the bold lettering, the rest is to show you can build complex queries this way.

No comments:

Post a Comment