Auto Increment

  • In testing with auto increment I havde noticed the ID field gets incremented even when the insert fails resulting in missing entries. I have tested on 2 build levels of SQL 2005 with bad input and got the same result. Is this working as advertised or is there a way for the incfrement only to occur on a successful insert.

  • yeah that's working by design;it's considered the normal behavior to throw away the identities that get rolled back in a transaciton or deleted; they are not reused.

    the identity() columns are just providing a unique integer, so they shouldn't have any business logic agaisnt them, like no gaps allowed or things like that.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • That's per-design.

    If you need sequential numbers for users, have that done in the presentation layer of the application, or at least in the query (using Row_Number, most likely). Don't use identity (auto-increment) columns for that.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • SPAM reported.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • jamesforest (3/21/2012)


    In testing with auto increment I havde noticed the ID field gets incremented even when the insert fails resulting in missing entries. I have tested on 2 build levels of SQL 2005 with bad input and got the same result. Is this working as advertised or is there a way for the incfrement only to occur on a successful insert.

    If SQL Server re-used identity values that were previously rolled back in a transaction, it would wreak havoc for guys who use READ UNCOMMITTED isolation level or NOLOCK hints on their queries.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • Eric M Russell (4/2/2012)


    jamesforest (3/21/2012)


    In testing with auto increment I havde noticed the ID field gets incremented even when the insert fails resulting in missing entries. I have tested on 2 build levels of SQL 2005 with bad input and got the same result. Is this working as advertised or is there a way for the incfrement only to occur on a successful insert.

    If SQL Server re-used identity values that were previously rolled back in a transaction, it would wreak havoc for guys who use READ UNCOMMITTED isolation level or NOLOCK hints on their queries.

    I don't think I follow you on this one Eric.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (4/2/2012)


    Eric M Russell (4/2/2012)


    jamesforest (3/21/2012)


    In testing with auto increment I havde noticed the ID field gets incremented even when the insert fails resulting in missing entries. I have tested on 2 build levels of SQL 2005 with bad input and got the same result. Is this working as advertised or is there a way for the incfrement only to occur on a successful insert.

    If SQL Server re-used identity values that were previously rolled back in a transaction, it would wreak havoc for guys who use READ UNCOMMITTED isolation level or NOLOCK hints on their queries.

    I don't think I follow you on this one Eric.

    It's problematic enough when one SPID (using NOLOCK) queries the ID for another SPID's half-baked uncommitted transaction. But when the transaction is rolled back, and then the same ID is recycled for another separte transaction, the potential for inconsistent results is even greater.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • Ahh I see what you are saying. Chalk up one more reason not to use dirty reads. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Jeff Moden (4/1/2012)


    SPAM reported.

    I must be missing something. What spam?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (4/2/2012)


    Jeff Moden (4/1/2012)


    SPAM reported.

    I must be missing something. What spam?

    There was a spam message in here at one point. After he reported it they took down that message. I have had that same happen a couple times.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (4/2/2012)


    GSquared (4/2/2012)


    Jeff Moden (4/1/2012)


    SPAM reported.

    I must be missing something. What spam?

    There was a spam message in here at one point. After he reported it they took down that message. I have had that same happen a couple times.

    Ah. Makes sense.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I suspect that the reason the ID values cannot be rolled back is because in order to roll them back, it would have to reseed the identity value and other queries in process may have already generated ID values above that. So the best it could do is to only roll back if there were no other transactions using the table, and then the behavior would be inconsistent. They would rather have consistent behavior.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply