Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. What happens with auto-incremented value on rollback?

What happens with auto-incremented value on rollback?

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadmin
24 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P piticcotoc

    I'm trying to find the answer to this question. Let's say you have a MSSQL database which has a table with an auto-incremented field. If I have a Sql transaction sent to the server (through code) that does an insert into that table and the transaction fails (and does a rollback) will the current ID be lost or not? When does it generate the new ID? At transaction start or finish? Ex: table1 has column ID as auto-increment. the current ID is 100. If I send a transaction with an insert does is reserve the ID 101 and loose it if the transaction fails with rollback, meaning next ID would be 102? Or will it take the ID 101 only if the transaction commits successfully? thanks

    J Offline
    J Offline
    Jorgen Andersson
    wrote on last edited by
    #2

    Not important. The point with autoincrementation is that your key field has unique values, and gaps in the sequence does not matter. And if it matters, you should probably rethink your design.

    People say nothing is impossible, but I do nothing every day.

    P 1 Reply Last reply
    0
    • J Jorgen Andersson

      Not important. The point with autoincrementation is that your key field has unique values, and gaps in the sequence does not matter. And if it matters, you should probably rethink your design.

      People say nothing is impossible, but I do nothing every day.

      P Offline
      P Offline
      piticcotoc
      wrote on last edited by
      #3

      Ok so you don't know. Anyone else?

      M R J 3 Replies Last reply
      0
      • P piticcotoc

        I'm trying to find the answer to this question. Let's say you have a MSSQL database which has a table with an auto-incremented field. If I have a Sql transaction sent to the server (through code) that does an insert into that table and the transaction fails (and does a rollback) will the current ID be lost or not? When does it generate the new ID? At transaction start or finish? Ex: table1 has column ID as auto-increment. the current ID is 100. If I send a transaction with an insert does is reserve the ID 101 and loose it if the transaction fails with rollback, meaning next ID would be 102? Or will it take the ID 101 only if the transaction commits successfully? thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        The new IDENTITY value is generated when a row insert is initiated and will update the table's current identity value even when the transaction is rolled back. In simple terms, it would behave as if no rollback happened.

        P 1 Reply Last reply
        0
        • P piticcotoc

          I'm trying to find the answer to this question. Let's say you have a MSSQL database which has a table with an auto-incremented field. If I have a Sql transaction sent to the server (through code) that does an insert into that table and the transaction fails (and does a rollback) will the current ID be lost or not? When does it generate the new ID? At transaction start or finish? Ex: table1 has column ID as auto-increment. the current ID is 100. If I send a transaction with an insert does is reserve the ID 101 and loose it if the transaction fails with rollback, meaning next ID would be 102? Or will it take the ID 101 only if the transaction commits successfully? thanks

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #5

          Identity (auto-increment) is not part of the transaction, so isn't rolled back. In your example, the ID for the next call would be 102.

          P 1 Reply Last reply
          0
          • P piticcotoc

            Ok so you don't know. Anyone else?

            M Offline
            M Offline
            Michael Potter
            wrote on last edited by
            #6

            piticcotoc wrote:

            Ok so you don't know. Anyone else?

            I believe he was trying to tell you that the answer to your question is useless information. Even if tests provided it one way, you could not rely on it to react the same between different database technologies or even between different versions of the same databases. It is not defined anywhere that I know of.

            1 Reply Last reply
            0
            • P piticcotoc

              Ok so you don't know. Anyone else?

              R Offline
              R Offline
              R Giskard Reventlov
              wrote on last edited by
              #7

              That's pretty rude: he gave you an answer.

              "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

              P 1 Reply Last reply
              0
              • I i j russell

                Identity (auto-increment) is not part of the transaction, so isn't rolled back. In your example, the ID for the next call would be 102.

                P Offline
                P Offline
                piticcotoc
                wrote on last edited by
                #8

                Thanks guys. Exactly what I needed to find out.

                1 Reply Last reply
                0
                • R R Giskard Reventlov

                  That's pretty rude: he gave you an answer.

                  "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                  P Offline
                  P Offline
                  piticcotoc
                  wrote on last edited by
                  #9

                  Rude? If I didn't need the answer I wouldn't have asked the question.

                  S R P P 4 Replies Last reply
                  0
                  • L Lost User

                    The new IDENTITY value is generated when a row insert is initiated and will update the table's current identity value even when the transaction is rolled back. In simple terms, it would behave as if no rollback happened.

                    P Offline
                    P Offline
                    piticcotoc
                    wrote on last edited by
                    #10

                    Thanks guys. Exactly what I needed to find out.

                    1 Reply Last reply
                    0
                    • P piticcotoc

                      Rude? If I didn't need the answer I wouldn't have asked the question.

                      S Offline
                      S Offline
                      Simon_Whale
                      wrote on last edited by
                      #11

                      Maybe we can chalk this down to a lost in translation but what Jorgen said is correct. Identity columns allow you to assign a unqiue value to each row. It doesn't get reset to the previous value if you rollback the transaction. If you require no gaps in your keys then you will need to think of a different approach.

                      Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                      1 Reply Last reply
                      0
                      • P piticcotoc

                        Rude? If I didn't need the answer I wouldn't have asked the question.

                        R Offline
                        R Offline
                        R Giskard Reventlov
                        wrote on last edited by
                        #12

                        Now you're just being a twat. He gave you an answer. You should have just moved on. What's the incentive for anyone else to help you in the future if they think you'll just be rude if you don't like the answer.

                        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                        P 1 Reply Last reply
                        0
                        • P piticcotoc

                          Ok so you don't know. Anyone else?

                          J Offline
                          J Offline
                          Jorgen Andersson
                          wrote on last edited by
                          #13

                          I do know, and I did tell you to rethink your design. If you had used Oracle it would have been much worse, Oracle cache the sequences, so if you restart the server you can get a gap of twenty numbers. and if you have a cluster server they won't even be in order. (<for the nitpicks> I know it's a setting, but if you want higher performance you don't change it</for the nitpicks>)

                          People say nothing is impossible, but I do nothing every day.

                          1 Reply Last reply
                          0
                          • P piticcotoc

                            Rude? If I didn't need the answer I wouldn't have asked the question.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #14

                            Contrarywise; you don't need to know and yet you asked anyway.

                            P 1 Reply Last reply
                            0
                            • P piticcotoc

                              I'm trying to find the answer to this question. Let's say you have a MSSQL database which has a table with an auto-incremented field. If I have a Sql transaction sent to the server (through code) that does an insert into that table and the transaction fails (and does a rollback) will the current ID be lost or not? When does it generate the new ID? At transaction start or finish? Ex: table1 has column ID as auto-increment. the current ID is 100. If I send a transaction with an insert does is reserve the ID 101 and loose it if the transaction fails with rollback, meaning next ID would be 102? Or will it take the ID 101 only if the transaction commits successfully? thanks

                              M Offline
                              M Offline
                              Mycroft Holmes
                              wrote on last edited by
                              #15

                              While Shameel and Russel gave you answers it is not the solution, if you are relying on the identity filed to give you sequential information (or any information) your design is WRONG. An ID field has only 1 function, to identify the record.

                              Never underestimate the power of human stupidity RAH

                              P 1 Reply Last reply
                              0
                              • P piticcotoc

                                Rude? If I didn't need the answer I wouldn't have asked the question.

                                P Offline
                                P Offline
                                pmpdesign
                                wrote on last edited by
                                #16

                                Why don't you just try it and find out for sure instead of being rude to people trying to help. :|

                                BEGIN TRANSACTION

                                INSERT something INTO table

                                ROLLBACK TRANSACTION

                                1 Reply Last reply
                                0
                                • R R Giskard Reventlov

                                  Now you're just being a twat. He gave you an answer. You should have just moved on. What's the incentive for anyone else to help you in the future if they think you'll just be rude if you don't like the answer.

                                  "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                                  P Offline
                                  P Offline
                                  piticcotoc
                                  wrote on last edited by
                                  #17

                                  I did move on. Until someone gave an answer to my question which was all i needed to know. Why do some people think that a question must always be answered with with an assumption that everything you do is wrong and you should change your thinking according to theirs? Did any of you even bothered to ask why I needed the answer to that question? (except the 2 straight to the point answers - thanks again). No you didn't. You just assumed it is something vital to what I do and you advised me to change my logic. So to sum it up and end this hopefully: - i got the answer I needed which is great - i was looking for an answer and not a solution - the mayans were wrong about the world end (along with whoever misunderstood what they were trying to say) - check above fact and see if you find any analogy with this now turning into a flame thread Thanks

                                  L R 2 Replies Last reply
                                  0
                                  • P PIEBALDconsult

                                    Contrarywise; you don't need to know and yet you asked anyway.

                                    P Offline
                                    P Offline
                                    piticcotoc
                                    wrote on last edited by
                                    #18

                                    Mind-reading is a great feature to have. This way you know what I want better than me... oh wait; you don't and yet you assumed otherwise anyway

                                    1 Reply Last reply
                                    0
                                    • P piticcotoc

                                      I did move on. Until someone gave an answer to my question which was all i needed to know. Why do some people think that a question must always be answered with with an assumption that everything you do is wrong and you should change your thinking according to theirs? Did any of you even bothered to ask why I needed the answer to that question? (except the 2 straight to the point answers - thanks again). No you didn't. You just assumed it is something vital to what I do and you advised me to change my logic. So to sum it up and end this hopefully: - i got the answer I needed which is great - i was looking for an answer and not a solution - the mayans were wrong about the world end (along with whoever misunderstood what they were trying to say) - check above fact and see if you find any analogy with this now turning into a flame thread Thanks

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #19

                                      piticcotoc wrote:

                                      Why do some people think that a question must always be answered with with an assumption that everything you do is wrong

                                      ..because we never use the value of an identity field; it does not (EVER) hold information. Asking what the number is indicates that you might indeed have a design-problem, and that might lead to problems in the long run. It's called free advice, and sometimes comes unsollicited.

                                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                                      P 1 Reply Last reply
                                      0
                                      • L Lost User

                                        piticcotoc wrote:

                                        Why do some people think that a question must always be answered with with an assumption that everything you do is wrong

                                        ..because we never use the value of an identity field; it does not (EVER) hold information. Asking what the number is indicates that you might indeed have a design-problem, and that might lead to problems in the long run. It's called free advice, and sometimes comes unsollicited.

                                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                                        P Offline
                                        P Offline
                                        piticcotoc
                                        wrote on last edited by
                                        #20

                                        :wtf:

                                        Quote:

                                        It's called free advice, and sometimes comes unsollicited.

                                        So do STDs but that doesn't mean I need them

                                        L 1 Reply Last reply
                                        0
                                        • P piticcotoc

                                          :wtf:

                                          Quote:

                                          It's called free advice, and sometimes comes unsollicited.

                                          So do STDs but that doesn't mean I need them

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #21

                                          Same goes for taxes, but the fact they have a single thing in common doesn't make them comparable. This is "normal behaviour" when asking an expert for advice. Ask a builder how to build on quicksand. You can expect an answer, and a warning. You can ask the doctor to remove your lungs - and get an answer and a warning. Same goes here. Even when being paid for the job, I will not shut up whn someone is making a mistake that could cost them dearly. I´m not that much of an ass yet :thumbsup:

                                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups