Saturday, November 20, 2010

Reset identity column index in SQL Server

Background
When you set a column in your SQL table to auto increment every inserted record will have a value of this column one number higher than the previous one - which is what we want in general. However even after you delete some or all of the records in the table the next inserted record will not have the next-higher-free-ID. It will be one higher than the last inserted one. Reseting this index is fortunately very easy and requires firing single command of a format:

Solution
DBCC CHECKIDENT ('table_name', RESEED, 0)
Where table_name is the name of the table you want to reset the auto increment ID on. The above command will reset the seed to the original value '0'. So the next inserted record will have the ID equal 1. Though you can replace the last parameter with a different number if you wish the numbering to start from a different position.

No comments:

Post a Comment