Adding an identity to an existing column

后端 未结 19 2094
温柔的废话
温柔的废话 2020-11-21 13:16

I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table.

I\'ve got a script to clean up the IDs to ensure

19条回答
  •  自闭症患者
    2020-11-21 14:06

    By design there is no simple way to turn on or turn off the identity feature for an existing column. The only clean way to do this is to create a new column and make it an identity column or create a new table and migrate your data.

    If we use SQL Server Management Studio to get rid of the identity value on column "id", a new temporary table is created, the data is moved to the temporary table, the old table is dropped and the new table is renamed.

    Use Management Studio to make the change and then right click in the designer and select "Generate Change Script".

    You will see that this is what SQL server in doing in the background.

提交回复
热议问题