Update SQL with consecutive numbering

前端 未结 10 600
庸人自扰
庸人自扰 2020-12-02 20:16

I want to update a table with consecutive numbering starting with 1. The update has a where clause so only results that meet the clause will be renumbered. Can I accomplish

10条回答
  •  隐瞒了意图╮
    2020-12-02 20:32

    UPDATE TableName
    SET TableName.id = TableName.New_Id
    FROM (
      SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS New_Id
      FROM TableName
      ) TableName
    

提交回复
热议问题