How do I reset an increment identity's starting value in SQL Server

三世轮回 提交于 2019-12-17 17:26:04

问题


I would like to have a nice template for doing this in development. How do I reset an increment identity's starting value in SQL Server?


回答1:


DBCC CHECKIDENT('TableName', RESEED, 0)



回答2:


Just a word of warning with:

DBCC CHECKIDENT (MyTable, RESEED, 0)

If you did not truncate the table, and the identity column is the PK, you will get an error when reaching pre-existing identites.

For example, you have identities (3,4,5) in the table already. You then reset the identity column to 1. After the identity 2 is inserted, the next insert will try to use the identity 3, which will fail.




回答3:


To set the identity to 100:

DBCC CHECKIDENT (MyTable, RESEED, 100)


来源:https://stackoverflow.com/questions/16971/how-do-i-reset-an-increment-identitys-starting-value-in-sql-server

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