I have an SQL table with a set of identifiers:
CREATE TABLE MyTable( MyTableID int IDENTITY(1,1) NOT NULL, RecordName nvarchar(100) NULL)
Something happened to this table, which led to an odd behavior. I need to find out that.
When the insert occurs:
INSERT MyTable(RecordName) VALUES('Test Bug') SELECT SCOPE_IDENTITY()
This is a problem because the code above this insertion expects the first identifier to be 1 - I cannot figure out how with IDENTITY(1,1) it ends up as 0 .
If (before executing INSERT ) I check the identifier, it returns null:
DBCC CHECKIDENT (MyTable, NORESEED)
Verification of identification information: the current value of the identifier is "NULL", the current value of the column is "NULL".
I know several ways to fix this; What do I need to know how the table got into this state in the first place?
The only way I know that CHECKIDENT returns null is if the table has just been created, but then IDENTITY(1,1) , and INSERT calls SCOPE_IDENTITY() 1 .
Alternatively, I can get 0 as the next identifier, if as a current seed ( DBCC CHECKIDENT (MyTable, RESEED, -1) or with SET IDENTITY_INSERT MyTable ON ) I force -1 , but then checks that the current -1 seed ( rather than zero), so this cannot be.
How did the database get into a state where the column has IDENTITY(1,1) , DBCC CHECKIDENT (MyTable, NORESEED) returns null, and the next INSERT calls SCOPE_IDENTITY() as 0 ?