Setting/Resetting Auto increment value in MSSQL 2005


Once you create a table and what to reset autoincrement value of a table to your desire number, you need to set some system table values, which are not available for direct editing. To perform this you need to do following:

SELECT OBJECT_ID,OBJECT_NAME(OBJECT_ID) AS TABLENAME, NAME AS COLUMNNAME, SEED_VALUE, INCREMENT_VALUE, LAST_VALUE, IS_NOT_FOR_REPLICATION FROM SYS.IDENTITY_COLUMNS ORDER BY 2

This statement simply show all table in your select table with auto increment value. Once this gives you good information, you can run following command to change value

DBCC CHECKIDENT(‘mytable’,RESEED,[new numeric value])

If this runs perfect give something like this:

Checking identity information: current identity value ’14’, current column value ’30’.DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Otherwise it give an error. (In above message 14 and 30 will be replaced with your values.

MSDN Link for more details : http://msdn2.microsoft.com/en-us/library/ms176057.aspx

, ,