Adding an identity to an existing column

后端 未结 19 2055
温柔的废话
温柔的废话 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 13:54

    Basically there are four logical steps.

    1. Create a new Identity column. Turn on Insert Identity for this new column.

    2. Insert the data from the source column (the column you wished to convert to Identity) to this new column.

    3. Turn off the Insert Identity for the new column.

    4. Drop your source column & rename the new column to the name of the source column.

    There may be some more complexities like working across multiple servers etc.

    Please refer the following article for the steps (using ssms & T-sql). These steps are intended for beginners with less grip on T-SQL.

    http://social.technet.microsoft.com/wiki/contents/articles/23816.how-to-convert-int-column-to-identity-in-the-ms-sql-server.aspx

提交回复
热议问题