Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Identity column

Author  Topic 

noamg
Posting Yak Master

215 Posts

Posted - 2012-05-29 : 10:37:08
Hi,

this is my table:

CREATE TABLE GamesApp.Users (
UserID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
CellularNum VARCHAR(20) NULL,
Email VARCHAR(200) NULL
)

the application call to SP, which run a simple insert statement:


INSERT INTO GamesApp.Users( CellularNum, Email )
VALUES( @CellularNum, @Email )
SET @UserID = SCOPE_IDENTITY()


sometime, I got a big gaps by the value in the identity column.
0
1
2
3
4
5
6
7
1002 <<< --- !!!
1003
1004

why ? any idea ?


Noam Graizer

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-05-29 : 11:00:44
If a value is inserted manually that will reset the seed. Also if an insert fails the identity value will be used up. Could be that a batch of rows is failing insert so you get a missing range.
Are any rows being deleted? That will not reset the seed.
An identity shouldn't be considered to have consecutive values unless the insert is in a single transaction
see
http://www.simple-talk.com/sql/t-sql-programming/identity-columns/

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

noamg
Posting Yak Master

215 Posts

Posted - 2012-05-29 : 11:26:15
no reset to the seek, no bulk or batch.
the only way to insert row is via the SP.

P.S.: SQL 2012


Noam Graizer
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-05-29 : 11:53:15
Then must doing an uncommited insert to give gaps - are there periods where it fails and something continually retries - that's the only way I can see you can get a missing range like that without someone changing the seed.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-29 : 12:21:51
I've read that SQL 2012 has a bug that can cause gaps if the server/service is reset. However, I've not been able to repro it myself.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-29 : 12:24:19
I couldn't find the other bug on this issue, but I did find this one. However, I'm not sure it applies in your case:
https://connect.microsoft.com/SQLServer/feedback/details/739013/alwayson-failover-results-in-reseed-of-identity

EDIT: Maybe this should be moved the SQL 2012 forum?
Go to Top of Page
   

- Advertisement -