Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 UPDATE table A with values FROM table B

Author  Topic 

beemd
Starting Member

14 Posts

Posted - 2013-10-28 : 16:19:53
Hi,

If I have two tables like this (simplified example)

dbo.a
id value
1 A
2 B
3 C

dbo.b
id value
1
2
3

I want to update b and set the value from a where the id's match? Could someone point me in the right direction?

Thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-28 : 16:48:51
You can do it like shown below:
UPDATE b SET 
value = a.VALUE
FROM
a INNER JOIN b ON a.id = b.id;
The only thing to be careful about is that if there is more than one row in table a for a given id in table b, it is unpredictable which of those rows in table a will be used to pick the value from to be used to update b
Go to Top of Page
   

- Advertisement -