F11 F11 - 6 months ago 22
SQL Question

Sql Update Query

I have a table

T1
which contains three columns:
Id, Name, Address


There is another table
T2
which contains 2 columns
Id, New_Address
.
Id
column of
T2
is same as of
T1
.

I need a query which will update
Address
column of
T1
with
New_Address
of
T2
.

I can do it through a loop by checking ID and executing update statement. How can it has to be done with a query?

Answer

How about

UPDATE T1
SET Address = T2.New_Address
FROM T2
WHERE T1.id = T2.id