I restore database and delete records in my_table using
delete from my_table;
Then I reseed table identity column using this query:
DBCC CHECKIDENT('dbo.my_table', reseed, 0);
and the message I got is:
Checking identity information: current identity value 'NULL', current column value '0'.
But, when I check current identity using this query:
DBCC CHECKIDENT ('my_table', NORESEED);
I got this message:
Checking identity information: current identity value 'NULL', current column value 'NULL'.
So, when I'm inserting record in table, first insert gives me an error, but if I try once more then insert succeed.
Why I can't set current identity column value to 0? I need first insert in table to be with identity value 1.
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 behaviour I was experiencing until I stumbled across the question above 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: