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 2012 Forums
 Transact-SQL (2012)
 Complicated UPDATE statement

Author  Topic 

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2014-05-17 : 09:06:38
I'm in need of a SQL statement that will use two fields from table A, match them to table B and then take the primary key from table B and write it to table A into the column of my choosing.

I hope that wasn't too convoluted...thanks in advance

Mike Brown

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-17 : 09:37:48
this would assume that only one row in B correlates to your two columns in A. And that you want to update your chosen column of the same row in table A rather than add a new row.

I hope that wasn't too convoluted a response.

update a set
a.myChosenColumn = b.PK
from tableA as a
join tableB as b
on b.col1 = a.col1
and b.col2 = a.col2


Be One with the Optimizer
TG
Go to Top of Page

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2014-05-17 : 17:11:02
That did the trick! ....thank you!!

Mike Brown
Go to Top of Page
   

- Advertisement -