dbTalk Databases Forums  

Inconsistent identity values

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Inconsistent identity values in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bill E.
 
Posts: n/a

Default Inconsistent identity values - 08-17-2007 , 09:04 PM






SQL Server 2005

We have a table where the current identity value is lower than the
maximum value. This is the first time I've seen such a thing. How
can this occur? I see postings about this problem but they seem to
concern older versions of SQL Server.

I did some tests on another table to see if I could reproduce the
problem by setting Identity_Insert on and then forcing the identity to
a value. I then set Identity_Insert off and added a row normally, but
the next identity was always the next largest number, not some number
in between.

Bill E.
Hollywood, FL


Reply With Quote
  #2  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: Inconsistent identity values - 08-18-2007 , 08:39 AM






Check out "DBCC CHECKIDENT" in BOL.

Gert-Jan


"Bill E." wrote:
Quote:
SQL Server 2005

We have a table where the current identity value is lower than the
maximum value. This is the first time I've seen such a thing. How
can this occur? I see postings about this problem but they seem to
concern older versions of SQL Server.

I did some tests on another table to see if I could reproduce the
problem by setting Identity_Insert on and then forcing the identity to
a value. I then set Identity_Insert off and added a row normally, but
the next identity was always the next largest number, not some number
in between.

Bill E.
Hollywood, FL

Reply With Quote
  #3  
Old   
Bill E.
 
Posts: n/a

Default Re: Inconsistent identity values - 08-19-2007 , 07:16 AM



I'm aware of this and it certainly is easy to fix, but it doesn't give
me insight into how this could have happened in the first place.

Bill

Gert-Jan Strik wrote:
Quote:
Check out "DBCC CHECKIDENT" in BOL.

Gert-Jan


"Bill E." wrote:

SQL Server 2005

We have a table where the current identity value is lower than the
maximum value. This is the first time I've seen such a thing. How
can this occur? I see postings about this problem but they seem to
concern older versions of SQL Server.

I did some tests on another table to see if I could reproduce the
problem by setting Identity_Insert on and then forcing the identity to
a value. I then set Identity_Insert off and added a row normally, but
the next identity was always the next largest number, not some number
in between.

Bill E.
Hollywood, FL


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Inconsistent identity values - 08-19-2007 , 07:21 AM



Bill E. (billmiami2 (AT) netscape (DOT) net) writes:
Quote:
I'm aware of this and it certainly is easy to fix, but it doesn't give
me insight into how this could have happened in the first place.
Look at this:

CREATE TABLE ident (a int IDENTITY (1, 1) PRIMARY KEY,
b char(2) NOT NULL UNIQUE)
go
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('XZ')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('CD')
go
DBCC CHECKIDENT(ident, RESEED, 5)
go
INSERT ident(b) VALUES ('EF')
SELECT ident_current('ident'), a, b FROM ident ORDER BY a
go
DROP TABLE ident

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #5  
Old   
Bill E.
 
Posts: n/a

Default Re: Inconsistent identity values - 08-19-2007 , 08:35 AM



That's fine Erland, but you had to reseed the identity in order to get
the next value to fall below an existing one.
I'll look into it but I seriously doubt that anyone did this. Is
there another way that this could happen?
For example, a sudden shutdown of the server or some kind of error in
a DTS process?

Bill


Erland Sommarskog wrote:
Quote:
Bill E. (billmiami2 (AT) netscape (DOT) net) writes:
I'm aware of this and it certainly is easy to fix, but it doesn't give
me insight into how this could have happened in the first place.

Look at this:

CREATE TABLE ident (a int IDENTITY (1, 1) PRIMARY KEY,
b char(2) NOT NULL UNIQUE)
go
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('XZ')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('AB')
INSERT ident (b) VALUES ('CD')
go
DBCC CHECKIDENT(ident, RESEED, 5)
go
INSERT ident(b) VALUES ('EF')
SELECT ident_current('ident'), a, b FROM ident ORDER BY a
go
DROP TABLE ident

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Inconsistent identity values - 08-19-2007 , 11:02 AM



Bill E. (billmiami2 (AT) netscape (DOT) net) writes:
Quote:
That's fine Erland, but you had to reseed the identity in order to get
the next value to fall below an existing one.
I'll look into it but I seriously doubt that anyone did this. Is
there another way that this could happen?
For example, a sudden shutdown of the server or some kind of error in
a DTS process?
How much below is it? In SQL 6.5 it was indeed a common problem that
the identity values fell behind when there was a unexpected shutdown,
but I have not seen this with SQL 2000 and SQL 2005. In any case, it
the value is just a little behind the max, one could suspect something
like that. But it if the difference is big, I rather place my bets on
DBCC CHECKIDENT or some other manoeuvre, although I can't think of one
right now.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.