Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

IDENTITY problem on Primary Key column in a table Expand / Collapse
Author
Message
Posted Sunday, July 26, 2009 1:51 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 4:30 AM
Points: 65, Visits: 136
Users are reporting failures to insert rows in a table. The table is little used and was thought to be working well until recently. On investigation, I find that the max. Primary Key of the table is 145. The PK column has an IDENTITY set up, with SEED = 1 and INCREMENT = 1. The Insert statement is trying to insert a row with PK = 128 and failing. SELECT IDENT_CURRENT('TableName') returns 127.

Does anyone know a quick and easy way out of this mess?

Thanks in advance.

Mark Thornton
Post #759807
Posted Sunday, July 26, 2009 2:08 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, May 30, 2013 1:26 PM
Points: 2,359, Visits: 3,293
DBCC CHECKIDENT() with RESEED option.



N 56°04'39.16"
E 12°55'05.25"
Post #759812
Posted Sunday, July 26, 2009 2:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:22 PM
Points: 11,789, Visits: 28,063
fastest way to fix it is to reseed the value with the next highest value...146 automatically:
dbcc checkident(identicheck,RESEED) --sets to max

i thought that could not happen, but sure enough, this example resets the value to 1, and would eventaully have an error on inserting #3 a second time.
create table identicheck( checkid int identity(1,1) primary key, checktext varchar(20) )
insert into identicheck(checktext) SELECT 'one'
set identity_insert identicheck on
insert into identicheck(checkid,checktext) SELECT 3,'three'
set identity_insert identicheck off

dbcc checkident(identicheck) --returns 3

dbcc checkident(identicheck,RESEED,1) --sets to 1

insert into identicheck(checktext) SELECT 'next' --inserts the value 2
insert into identicheck(checktext) SELECT 'next' --should insert the value 3, crashes
select * from identicheck

drop table identicheck



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #759815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse