views:

558

answers:

4

Let's say there are 2 tables Table1 { ID, Name, Other } and Table2 { ID, Name, Other }. Both of them have identical records with the same IDs except that in Table1 all Name values are NULL. How can I import Name values from Table2 to Table1 using T-SQL (SQL Server 2008)?

A: 

You're looking for the MERGE command, which is like the UPSERT that you've probably read about elsewhere. Here's a quick article about it.

Dillie-O
A: 
UPDATE Table1
SET Table1.Name = Table2.Name
FROM Table2
WHERE Table1.Id = Table2.Id
--AND Table1.Name IS NULL
Jhonny D. Cano -Leftware-
+4  A: 
Update Table1
Set Table1.Name = Table2.Name
From
Table1 INNER JOIN Table2 on Table1.ID = Table2.ID
Jason Punyon
Thanks, I knew it should be simple :)
Tarkus
@unknown (google): no problem.
Jason Punyon
A: 

Just join the tables and update:

update t1
set [Name] = t2.Name
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID
Guffa