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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating data in table A where record exists in table B 1

Status
Not open for further replies.

awaria

IS-IT--Management
Sep 16, 2010
100
US
I need help with a query. SQL Server 2000

I need to update the field [description] in table A with the
value in field [description] from table B, where record exists in both tables.

Appreciate your assistance.

Thank you,

Andrew


 
Update Tablea
Set Tablea.description=TableB.description
from Tablea
inner join Tableb
on Tablea.xxx=TableB.xxx
And Tablea.yyy=TableB.yyy
And Tablea.zzz=TableB.zzz
 
You'll need to define "where record exists in both tables".

The simplest syntax for this would be...

Code:
Update TableA
Set    TableA.description = TableB.Description
From   TableA
       Inner Join TableB
         On TableA.IdColumn = TableB.IdColumn

Notice the ON clause. You'll need to define how the rows from each table are matched. Also, it's usually best to write a select query before actually updating the data. Something like this...

Code:
--Update TableA
--Set    TableA.description = TableB.Description
Select TableA.IdColumn, 
       TableA.Description, 
       TableB.Description
From   TableA
       Inner Join TableB
         On TableA.IdColumn = TableB.IdColumn

Once you get the select working (to show the correct data), comment the select and uncomment the Update and Set lines.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you for the tips.

I have one more related query to do. How do I select [description] from table B where table B recordID equals table A recordID?

Thanks for all your help

Andrew
 
Select b.description
from tableb b
Inner Join tablea a ON a.recordid = b.recordid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top