Swap values for two rows in the same table in SQL Server

后端 未结 8 2003
北海茫月
北海茫月 2020-12-17 14:59

I want to swap the values from two rows in a table. I have the rows IDs of the two rows. Is there any query to do that? Here is an example. Before the query I have this:

8条回答
  •  醉话见心
    2020-12-17 15:06

    Simple update works:

    UPDATE myTable
    SET 
    col1 = CASE WHEN col1 = 1 THEN 5 ELSE 1 END,
    col2 = CASE WHEN col2 = 2 THEN 6 ELSE 2 END,
    col3 = CASE WHEN col3 = 3 THEN 7 ELSE 3 END 
    

    Result: row values are swapped.

提交回复
热议问题