Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How-To Update Tables Using Inner Joins

T-SQL Hints and Tips

How-To Update Tables Using Inner Joins

by  MapMan  Posted    (Edited  )
You've gone through the trouble of creating a select statement that joins a couple of tables together and discovered that some data needs to be updated. How can you do that when you've joined tables?

First, state that you're going to be performing an update...
Code:
UPDATE
Then join the tables that will meet the condition.
Code:
TABLEA AS A INNER JOIN TABLEB AS B ON A.F1=B.F1
Then state what's going to change.
Code:
SET A.X = 1, B.Y = 2
Then we add a WHERE clause.
Code:
WHERE A.Z =10
Tie it all together...
Code:
UPDATE TABLEA AS A INNER JOIN TABLEB AS B ON A.F1=B.F1
  SET A.X = 1, B.Y = 2 WHERE A.Z =10
Problem solved.

Good Luck,

MapMan [americanflag]


Assume nothing, question everything, be explicit not implicit, and you'll always be covered
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top