MSSQL: Updating a row with data from another row – in the same table

Let’s say you have a number of rows in the my_table. Now for some reason, you want to update a row in my_table with data from another row. Here’s a not-very-obvious recipe of how to do it:

UPDATE my_table
SET
my_table.col1 = m.col1,
my_table.col22 = m.col2
FROM my_table m
INNER JOIN my_table
ON my_table.id = <destination_id>
AND m.id = <source_id>

Good luck!

Leave a Reply