A simple and quick snippet of SQL to update table values based on the values stored in another table — in other words, UPDATE SELECT. This code has only been tested in MySQL, similar code may be used in MSSQL or other database engines which I will try and add later.
UPDATE SELECT using JOIN Model:
UPDATE Dest
SET Dest.Address1 = Src.Address1,
Dest.Address2 = Src.Address2
FROM SourceTableName Src
JOIN DestinationTableName Dest ON Dest.PersonID = Src.PersonID
UPDATE SELECT using WHERE Model:
UPDATE DestinationTableName Dest, SourceTableName Src
SET Dest.Address1 = Src.Address1,
Dest.Address2 = Src.Address2
WHERE Dest.PersonID = Src.PersonID
Comments (1)
RSS Collapse / ExpandI've got one better example of UPDATE ;)
jmail
Only registered and authorized users can leave comments. Login or Register