Updating Relational SQL Tables
Wednesday, March 25, 2009 at 03:38PM 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

Reader Comments