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

Sql ServerIdentity

Sql Server Problem Overview


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?

Sql Server Solutions


Solution 1 - Sql Server

DBCC CHECKIDENT('TableName', RESEED, 0)

Solution 2 - Sql Server

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.

Solution 3 - Sql Server

To set the identity to 100:

DBCC CHECKIDENT (MyTable, RESEED, 100)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoel MeadorView Question on Stackoverflow
Solution 1 - Sql ServerRyan SampsonView Answer on Stackoverflow
Solution 2 - Sql ServerForgotten SemicolonView Answer on Stackoverflow
Solution 3 - Sql ServerKeithView Answer on Stackoverflow