|
|
|
Valued 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
|
|
|
|
|
SSCrazy
      
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"
|
|
|
|
|
SSChampion
        
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
|
|
|
|