23/12/2009

SQL - Update a table from another table

Just a quick blog mainly for my purposes, if you need to run a SQL update on a table and use another table for reference information e.g. updating prices for 2010 products e.t.c. then you can use the code below.


UPDATE destination_tbl
SET destination_column = (SELECT source_tbl.source_column
FROM source_tbl
WHERE source_tbl.criteria = destination_tbl.criteria)
WHERE EXISTS
(SELECT source_tbl.source_column
FROM source_tbl
WHERE source_tbl.criteria = destination_tbl.criteria)

No comments:

Post a Comment