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
2009-04-06 21:54:25
A:
UPDATE Table1
SET Table1.Name = Table2.Name
FROM Table2
WHERE Table1.Id = Table2.Id
--AND Table1.Name IS NULL
Jhonny D. Cano -Leftware-
2009-04-06 21:55:11
+4
A:
Update Table1
Set Table1.Name = Table2.Name
From
Table1 INNER JOIN Table2 on Table1.ID = Table2.ID
Jason Punyon
2009-04-06 21:55:21
Thanks, I knew it should be simple :)
Tarkus
2009-04-06 21:59:31
@unknown (google): no problem.
Jason Punyon
2009-04-06 22:08:04
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
2009-04-06 21:57:01