简体   繁体   中英

SQL Server identity column inserted 0 value with seed of 1

This is somewhat related to question: SQL server identity column values start at 0 instead of 1

But I'm not sure what happening here. We have our "blank" database which we use to start customer's accounts. We do that by backing up and restoring database.

Customer gets completely blank tables after restore. Example of table, all our tables have PKs exactly like this one:

CREATE TABLE [dbo].[RNPRatePlanStop]
(
    [RatePlanStopKey] [int] IDENTITY(1,1) NOT NULL,
    [RatePlanKey] [int] NOT NULL,   
    .....
)

Once in a while (not always and not on all tables) when new record inserted - 0 not expected 1 inserted. I'm not sure how this can be fixed or what I'm doing wrong. This is causing problems in UI because we don't count on having 0 keys..

Also declaration is IDENTITY(1,1) when I run command like this:

SELECT IDENT_CURRENT('RNPRatePlanStop')

It returned 0. For this table and about 7 other tables. Don't know how that was possible, maybe because IDENTITY was applied later via script.

Solution was to "seed" proper identity on those tables:

DBCC CHECKIDENT(RNPRatePlanStop, RESEED, 1)

After that - they all reset correctly and all is well. Still a mistery how those few out of a 100+ tables ended up like so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM