Rearrangement of two rows in MS SQLServer with preservation of the original primary key and without manual update

I have the following problem: I have lines like

ID   CODE     NAME            .........
1    h1100h1  Cool example1   .........
2    h654441  Another cool1   .........

I would like to change them, retaining all the old primary keys and restrictions. Of course, I can easily solve this manually by updating the lines. I'm just wondering if anyone has a great solution for this kind of problem, and not just running the update command manually. I really really appreciate any suggestions or recommendations.

+3
source share
2 answers

, , . , id . , .

UPDATE
     T1
SET
     column_1 = T2.column_1,
     column_2 = T2.column_2,
     ...
FROM
     dbo.My_Table T1
INNER JOIN dbo.My_Table T2 ON
     T2.id =
          CASE
               WHEN T1.id = @id_1 THEN @id_2
               WHEN T1.id = @id_2 THEN @id_1
               ELSE NULL
          END
WHERE
     T1.id IN (@id_1, @id_2)
+6

-

-1

Source: https://habr.com/ru/post/1702310/


All Articles