« Using Modal Popup and Rounded Corners Extenders in Conjunction | Main | Using T-SQL 'TOP' Clause with Variables »
Wednesday
25Mar2009

Updating Relational SQL Tables

Updating a table with data from another table in the same database is something I find myself doing pretty frequently – I use this example quite a bit so I thought I’d post it for easy reference. It facilitates the updating of one table with data from another table, using a key relationship:

 

Take for example TableA with the following columns…

 

ModelId (PK)

CarName

VehicleDescription

CarSerialId

 

And TableB with the following columns…

 

MaintenanceId (PK)

ModelId (FK)

MaintenanceHist

OwnerName

CarDescription

 

If I wanted to update the CarDescription column in TableB with the data in the VehicleDescription column in Table A, joining on ModelId, this is how it would be done:

 

UPDATE [DatabaseName].[dbo].[TableB]

SET CarDescription = TableA.VehicleDescription

FROM TableB, TableA

WHERE TableB.ModelId = TableA.ModelId

 

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>