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. Identity behavior in sql server 2008 and 2012

Identity behavior in sql server 2008 and 2012

Scheduled Pinned Locked Moved Database
questiondatabasesql-serversysadminjson
8 Posts 4 Posters 4 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.
  • V Offline
    V Offline
    VK19
    wrote on last edited by
    #1

    Hi - We migrated from sql server 2008 to 2012. We have a stored proc which inserts dates into a table. We insert an initial value wherein the identity key value is -1, with an insert statement. For the rest of the dates, we have a while loop. within the while loop, the next identity key value is resulting as 0 in sql server 2012 and as a 1 in sql server 2008. Both server have the table's field name defined with identity_seed as 0, and increment as 1. Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1? Thank you.

    CHill60C Richard DeemingR J 3 Replies Last reply
    0
    • V VK19

      Hi - We migrated from sql server 2008 to 2012. We have a stored proc which inserts dates into a table. We insert an initial value wherein the identity key value is -1, with an insert statement. For the rest of the dates, we have a while loop. within the while loop, the next identity key value is resulting as 0 in sql server 2012 and as a 1 in sql server 2008. Both server have the table's field name defined with identity_seed as 0, and increment as 1. Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1? Thank you.

      CHill60C Offline
      CHill60C Offline
      CHill60
      wrote on last edited by
      #2

      The fact you have a while-loop is filling me horror. However, without being able to see the SP code we can't possibly comment on what might be going wrong. You have contradicted yourself however, you stated

      Quote:

      We insert an initial value wherein the identity key value is -1,

      but you state

      Quote:

      Both server have the table's field name defined with identity_seed as 0, and increment as 1.

      Without checking I can't confirm, but perhaps there was a hole in 2008 that allowed you to trample all over the IDENTITY column. You should use IDENTITY (-1,1) if you want the first key to be -1 (but I suspect you don't). Try posting the code so we can suggest ways of improving it ... like getting rid of the WHILE loop :laugh:

      1 Reply Last reply
      0
      • V VK19

        Hi - We migrated from sql server 2008 to 2012. We have a stored proc which inserts dates into a table. We insert an initial value wherein the identity key value is -1, with an insert statement. For the rest of the dates, we have a while loop. within the while loop, the next identity key value is resulting as 0 in sql server 2012 and as a 1 in sql server 2008. Both server have the table's field name defined with identity_seed as 0, and increment as 1. Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1? Thank you.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Sounds like you're RESEEDing the table:

        DBCC CHECKIDENT (Transact-SQL) | Microsoft Docs[^]:

        If rows are present in the table, the next row is inserted with the new_reseed_value value. In version SQL Server 2008 R2 and earlier, the next row inserted uses new_reseed_value + the current increment value.

        So if you reseed to 0, and your increment is 1, the next value in SQL 2008 R2 or earlier will be 1, whereas the next value in SQL 2012 or later will be 0.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        CHill60C V 2 Replies Last reply
        0
        • Richard DeemingR Richard Deeming

          Sounds like you're RESEEDing the table:

          DBCC CHECKIDENT (Transact-SQL) | Microsoft Docs[^]:

          If rows are present in the table, the next row is inserted with the new_reseed_value value. In version SQL Server 2008 R2 and earlier, the next row inserted uses new_reseed_value + the current increment value.

          So if you reseed to 0, and your increment is 1, the next value in SQL 2008 R2 or earlier will be 1, whereas the next value in SQL 2012 or later will be 0.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          CHill60C Offline
          CHill60C Offline
          CHill60
          wrote on last edited by
          #4

          Nice!

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Sounds like you're RESEEDing the table:

            DBCC CHECKIDENT (Transact-SQL) | Microsoft Docs[^]:

            If rows are present in the table, the next row is inserted with the new_reseed_value value. In version SQL Server 2008 R2 and earlier, the next row inserted uses new_reseed_value + the current increment value.

            So if you reseed to 0, and your increment is 1, the next value in SQL 2008 R2 or earlier will be 1, whereas the next value in SQL 2012 or later will be 0.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            V Offline
            V Offline
            VK19
            wrote on last edited by
            #5

            Thank you, that is helpful!

            1 Reply Last reply
            0
            • V VK19

              Hi - We migrated from sql server 2008 to 2012. We have a stored proc which inserts dates into a table. We insert an initial value wherein the identity key value is -1, with an insert statement. For the rest of the dates, we have a while loop. within the while loop, the next identity key value is resulting as 0 in sql server 2012 and as a 1 in sql server 2008. Both server have the table's field name defined with identity_seed as 0, and increment as 1. Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1? Thank you.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              VK19 wrote:

              Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1?

              The answer was above, but I am curious why it matters. Certainly existing data would long be past zero or one. And if this is new data, always, then why does it matter?

              V 1 Reply Last reply
              0
              • J jschell

                VK19 wrote:

                Why does sql server 2008 have 1 as the next value after -1, and sql server 2012 have 0 as the next value after -1?

                The answer was above, but I am curious why it matters. Certainly existing data would long be past zero or one. And if this is new data, always, then why does it matter?

                V Offline
                V Offline
                VK19
                wrote on last edited by
                #7

                This is part of our datawarehouse project. We have some reports set up for data extract, and using some <> 0 conditions.

                J 1 Reply Last reply
                0
                • V VK19

                  This is part of our datawarehouse project. We have some reports set up for data extract, and using some <> 0 conditions.

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  So then adjust your index to start at 100 just to be sure.

                  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