Many time a situation comes where we would like to reset identity column values like for testing u inserted few records and now u would like to reset or you have deleted good amount of records and u would like to re-start from the current max value after deletion. Recently I had this requirement, every time we test project we wanted to reset identity values. stupidly we where just running the big whole generated SQL script. The Transact-SQL's DBCC statements gave me an alternative approach and now I see that things can be done programmatically also.
DBCC stands for Database Console Command statements: DBCC CHECKIDENT is used to Checks the current identity value for the specified table and, if needed, corrects the identity value. Simply issue a command like this:
USE TrainingDB
GO
DBCC CHECKIDENT (MyOrders, RESEED, 1)
GO
This SQL code will forcibly reset the seed value to 1 for the specified table.
Oracle has similar thing called for Auto increment called "Sequence" and it has 2 method S1%Nextval and s1%CurrentVal so with this is possible to find out what is the current auto increment value, for some reasons u would like to find out same thing in SQLServer then
This example reports the current identity value in the jobs table, and does not correct the identity value, if incorrect.
USE pubs
GO
DBCC CHECKIDENT (jobs, NORESEED)
GO
Checking identity information: current identity value '7', current column value '7'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
The out put is a string so some string parsing is required here. not tried any other stunt so if anyone knows something more do write me back.