Categories
SQL Server

Updating multiple table rows using a query

So you made a modification to a table and want to update many rows with values from a different table.

If you want to just insert new rows, you do:

INSERT INTO table1
SELECT val1, val2, val3
FROM table2
WHERE some condition

You need to be sure that the columns used in the insert correspond to the rows of the table you insert into.

Still, if you want to update, the syntax is slightly different:

UPDATE table1
SET col1 = t2.col3,
col2 = t2.col4
FROM table1 t1, table2 t2
WHERE t1.t1_id = t2.t1_id

Now I know…

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Share