I\'m using this code to reset the identity on a table:
DBCC CHECKIDENT(\'TableName\', RESEED, 0)
This works fine most of the time, with the f
You are right in what you write in the edit of your question.
After running DBCC CHECKIDENT('TableName', RESEED, 0)
:
- Newly created tables will start with identity 0
- Existing tables will continue with identity 1
The solution is in the script below, it's sort of a poor-mans-truncate :)
-- Remove all records from the Table
DELETE FROM TableName
-- Use sys.identity_columns to see if there was a last known identity value
-- for the Table. If there was one, the Table is not new and needs a reset
IF EXISTS (SELECT * FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = 'TableName' AND last_value IS NOT NULL)
DBCC CHECKIDENT (TableName, RESEED, 0);