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 first insert I do inserting 1 into the Id column. However, if I drop the DB and recreate it (using scripts I’ve written) and then call DBCC CHECKIDENT, the first item inserted will have an ID of 0.
Any ideas?
EDIT: After researching I found out I didn’t read the documentation properly – "The current identity value is set to the new_reseed_value. If no rows have been inserted to the table since it was created, the first row inserted after executing DBCC CHECKIDENT will use new_reseed_value as the identity. Otherwise, the next row inserted will use new_reseed_value + 1. "
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 🙂
I did this as an experiment to reset the value to 0 as I want my first identity column to be 0 and it’s working.
It seems ridiculous that you can’t set/reset an identity column with a single command to cover both cases of whether or not the table has had records inserted. I couldn’t understand the behavior I was experiencing until I stumbled across this question on SO!
My solution (ugly but works) is to explicitly check the
sys.identity_columns.last_value
table (which tells you whether or not the table has had records inserted) and call the appropriateDBCC CHECKIDENT
command in each case. It is as follows:I have used this in SQL to set IDENTITY to a particular value:-
And this in C# to set a particular value:-
This builds on the above answers and always makes sure the next value is 42 (in this case).
Simply do this:
Borrowing from Zyphrax’s answer …
Caveats:
This is intended for use in reference data population situations where a DB is being initialized with enum type definition tables, where the ID values in those tables must always start at 1. The first time the DB is being created (e.g. during SSDT-DB publishing) @Reseed must be 0, but when resetting the data i.e. removing the data and re-inserting it, then @Reseed must be 1. So this code is intended for use in a stored procedure for resetting the DB data, which can be called manually but is also called from the post-deployment script in the SSDT-DB project. In that way the reference data inserts are only defined in one place but aren’t restricted to be used only in post-deployment during publishing, they are also available for subsequent use (to support dev and automated test etc.) by calling the stored procedure to reset the DB back to a known good state.
Change statement to
This will start from 2 (or 1 when you recreate table), but it will never be 0.
As you pointed out in your question it is a documented behavior. I still find it strange though. I use to repopulate the test database and even though I do not rely on the values of identity fields it was a bit of annoying to have different values when populating the database for the first time from scratch and after removing all data and populating again.
A possible solution is to use truncate to clean the table instead of delete. But then you need to drop all the constraints and recreate them afterwards
In that way it always behaves as a newly created table and there is no need to call DBCC CHECKIDENT. The first identity value will be the one specified in the table definition and it will be the same no matter if you insert the data for the first time or for the N-th
See also here: http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/06/26/fun-with-dbcc-chekident.aspx
This is documented behavior, why do you run CHECKIDENT if you recreate the table, in that case skip the step or use TRUNCATE (if you don’t have FK relationships)