How to change a column to an IDENTITY column and preserve the data?

假如想象 提交于 2020-01-02 08:34:32

问题


I had a column which was IDENTITY(1,1). All rows had been numbered consecutively. I did not add or remove any rows. I removed the identity property on this column. Now, I want it back. I think i can reset it to identity with seed = biggest number in existing table.

Could any problems arise if I do this ?


回答1:


From MSDN SQL Server forum - You have 2 options:

  1. Create a new table with identity & drop the existing table
  2. Create a new column with identity & drop the existing column.

*But take special care when these columns have any constraints / relations

As stated in MSDN documentation about considerations for preserving data during deployment and schema Updates:

Changes to properties of an identity column on a table, such as the identity seed - "Requires data motion" and it can, in some cases, cause data loss during its execution of the synchronization.

The recommendation in this case: "If the deployment or update script contains these types of changes, you might want to modify it manually to preserve the data"



来源:https://stackoverflow.com/questions/20177962/how-to-change-a-column-to-an-identity-column-and-preserve-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!