SQL Server: how to add new identity column and populate column with ids?

前端 未结 2 814
我在风中等你
我在风中等你 2020-12-13 03:21

I have a table with huge amount of data. I\'d like to add extra column id and use it as a primary key. What is the better way to fill this column with values fr

2条回答
  •  无人及你
    2020-12-13 04:06

    If you want to add row numbers in a specific order you can do ROW_NUMBER() into a new table then drop the original one. However, depending on table size and other business constraints, you might not want to do that. This also implies that there is a logic according to which you will want the table sorted.

    SELECT ROW_NUMBER() OVER (ORDER BY COL1, COL2, COL3, ETC.) AS ID, *
    INTO NEW_TABLE
    FROM ORIGINAL_TABLE
    

提交回复
热议问题