Saturday, 18 January 2014

SQL Server - Reset Identity Column Value to 1 in SQL Database

Here I will explain how to reset identity column value in SQL server or change or rest identity column value to 1 in SQL server. or reseed identity column value in SQL server.
Description:

In previous post I explained how set identity or auto incrementcolumn in SQL server. After set identity property on particular column I inserted few records in table and that value automatically increase whenever I inserted data that would be like this


In one situation I deleted all existing records and tried to insert new records in table during that time identity column value starting from previous increased value

Ex: Above table contains 8 records after delete all the records if I insert new record CountryID value will start from 9.

To reset identity column value and start value from “1” during insert new records we need to write query to reset identity column value. Check below Query


DBCC CHECKIDENT (Table_Name, RESEED, New_Reseed_Value)

Table_Name is name of your identity column table

RESEED specifies that the current identity value should be changed.

New_Reseed_Value is the new value to use as the current value of the identity column.   
  

EXDBCC CHECKIDENT ('UserDetails', RESEED, 0)

Once we run the above query it will reset the identity column in UserDetails table and starts identity column value from “1

No comments:

Post a Comment