How do I UPDATE from a SELECT in SQL Server?
UPDATE Table_ASET Table_A.col1 = Table_B.col1, Table_A.col2 = Table_B.col2FROM Some_Table AS Table_A INNER JOIN Other_Table AS Table_B ON Table_A.id = Table_B.idWHERE Table_A.col3 = 'cool'
In SQL Server 2008 (or newer), use MERGE
MERGE INTO YourTable T USING other_table S ON T.id = S.id AND S.tsql = 'cool'WHEN MATCHED THEN UPDATE SET col1 = S.col1, col2 = S.col2;
Alternatively:
MERGE INTO YourTable T USING ( SELECT id, col1, col2 FROM other_table WHERE tsql = 'cool' ) S ON T.id = S.idWHEN MATCHED THEN UPDATE SET col1 = S.col1, col2 = S.col2;
UPDATE YourTable SET Col1 = OtherTable.Col1, Col2 = OtherTable.Col2 FROM ( SELECT ID, Col1, Col2 FROM other_table) AS OtherTableWHERE OtherTable.ID = YourTable.ID