Wednesday, April 4, 2012

SQL Server Reset identity column value of table

Introduction: 
In this article I will explain how to reset identity column value of table in in 
SQL server.

Description:
After set identity property on particular column(StudentId) I inserted few records in Student table and that value(StudentId) automatically increase whenever I inserted data that would be like this.Here I have inserted some Name of students.



Now I am Going to deleted all existing records and then tried to insert new records in table.Now if you can see that identity column value starting from previous increased value Ex: Above table contains 5 records after delete all the records if I insert new record StudentId value will start from 6.
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) where
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 ('Student', RESEED, 0)
Once we run the above query it will reset the identity column in Student table and starts identity column value from “1

0 comments:

Post a Comment