UPDATE table FROM table2

How Can We Help?

You are here:
< Back

The simple Way to copy the content from one table to other is as follow:

UPDATE table2 
SET table2.col1 = table1.col1, 
table2.col2 = table1.col2,
...
FROM table1, table2 
WHERE table1.memberid = table2.memberid

For SQL Server 2008 + Using MERGE rather than the proprietary UPDATE ... FROM syntax has some appeal.

As well as being standard SQL and thus more portable it also will raise an error in the event of there being multiple joined rows on the source side (and thus multiple possible different values to use in the update) rather than having the final result be undeterministic.

MERGE INTO table1 as a
   USING table2 as b
      ON a.col2 = b.col2
WHEN MATCHED THEN
   UPDATE 
      SET a.col1 = b.col1;

Note the ; at the very end.

http://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match