How to close sequence gaps in SQL Server?

后端 未结 4 1079
囚心锁ツ
囚心锁ツ 2021-01-22 20:02

Let\'s say I have a Turtle table. When I run

SELECT * FROM Turtle ORDER BY Sort

I get this:

Id | Name | Sort
2    Leo    1
3           


        
4条回答
  •  悲哀的现实
    2021-01-22 20:57

    You can do the update just using a CTE with the row_number(), and then just update the CTE:

    ;with CTE as (
      select *, row_number () over (order by Sort) as RN
      from Turtle
    )
    
    update CTE
    set Sort = RN
    

提交回复
热议问题