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. How to recover identity ID after insert in MSSQL 2008?

How to recover identity ID after insert in MSSQL 2008?

Scheduled Pinned Locked Moved Database
questiondatabasesql-serveralgorithmstutorial
39 Posts 8 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 PIEBALDconsult

    I probably wouldn't; it sounds like a bad idea -- there are times when a developer needs to tell management that an idea is stoooopid. Otherwise, same way -- isn't there a ClientID field as well as a Created field? Or use a custom GUID with such things embedded within it.

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

    PIEBALDconsult wrote:

    Or use a custom GUID with such things embedded within it

    So your solution is to replicate existing functionality.

    P 1 Reply Last reply
    0
    • M Mycroft Holmes

      I said the PK should NOT be used for anything but the identification of the record.

      jschell wrote:

      From that one then must decide on a way to provide that ordering. And PK integer might be used for that that.

      If ordering is required then add in a specific field to service that requirement, DON'T use the PK field.

      jschell wrote:

      Not arbitrary rules

      There is absolutely nothing arbitrary about these rules. If you don't own the data in the field (it is generated by the database) then you don't control the data. Sybase would randomly skip a few 10k counters, some database system would fill in the gaps, some developers want to fill the gaps.

      Never underestimate the power of human stupidity RAH

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

      Mycroft Holmes wrote:

      I said the PK should NOT be used for anything but the identification of the record.

      You certainly went on to say that. However before doing that you quoted a specific statement and then said it was wrong. And what you quoted was not wrong.

      Mycroft Holmes wrote:

      If ordering is required then add in a specific field to service that requirement, DON'T use the PK field.

      So you are suggesting that one should add a unique sequential value which proceeds in lockstep with the primary key. And one must ignore EVERY other consideration in making that decision, such as complexity, cost, record size, project size and every other possible consideration. Myself I like to base my designs on the business needs and use best practices to make informed decisions as to what what might be best rather than accepting them as commandments from a supreme being.

      Mycroft Holmes wrote:

      There is absolutely nothing arbitrary about these rules.

      It certainly is. If not quote three authorative references that state exactly that.

      Mycroft Holmes wrote:

      Sybase would randomly skip a few 10k counters, some database system would fill in the gaps, some developers want to fill the gaps.

      And I am rather certain that at one time there was no way to generate a GUID as an intrinsic key in Oracle. But the limitations of a specific database or version has nothing at all to do with anything.

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        jschell wrote:

        was the table that you had difficulty with designed to be copied from one location to another while maintaining foreign keys

        Well, I was using the tool built by the infrastructure team for copying data between dev/test/prod, it's the only way to do it... so, yeah, obviously not.

        jschell wrote:

        any developer that is even close to being able to tell the future

        But you should know the past and learn from it.

        jschell wrote:

        So which of those alternatives are you suggesting business developers should rely on?

        Don't use identity/auto-increment columns, because you never know what the future holds.

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

        PIEBALDconsult wrote:

        Well, I was using the tool built by the infrastructure team for copying data between dev/test/prod, it's the only way to do it... so, yeah, obviously not.

        So you ran into a poor design. That of course has nothing to do with a specific technological implementation.

        PIEBALDconsult wrote:

        But you should know the past and learn from it

        Correct - when one creates a design that doesn't actually meet the known needs of the business then one should learn to create designs that do meet the needs. Don't see that that has anything to do with this discussion.

        PIEBALDconsult wrote:

        Don't use identity/auto-increment columns, because you never know what the future holds.

        No that isn't the conclusion. The conclusion is that you were dealing with a design that didn't meet the known business needs. Nothing more. There are an infinite number of ways to fail in that regard using absolutely any technology in absolutely any way.

        P 1 Reply Last reply
        0
        • J jschell

          PIEBALDconsult wrote:

          Or use a custom GUID with such things embedded within it

          So your solution is to replicate existing functionality.

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #33

          No, not really.

          1 Reply Last reply
          0
          • J jschell

            PIEBALDconsult wrote:

            Well, I was using the tool built by the infrastructure team for copying data between dev/test/prod, it's the only way to do it... so, yeah, obviously not.

            So you ran into a poor design. That of course has nothing to do with a specific technological implementation.

            PIEBALDconsult wrote:

            But you should know the past and learn from it

            Correct - when one creates a design that doesn't actually meet the known needs of the business then one should learn to create designs that do meet the needs. Don't see that that has anything to do with this discussion.

            PIEBALDconsult wrote:

            Don't use identity/auto-increment columns, because you never know what the future holds.

            No that isn't the conclusion. The conclusion is that you were dealing with a design that didn't meet the known business needs. Nothing more. There are an infinite number of ways to fail in that regard using absolutely any technology in absolutely any way.

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #34

            Ummmm... so how do you copy records between tables that have identity columns? :confused:

            J 1 Reply Last reply
            0
            • P PIEBALDconsult

              Ummmm... so how do you copy records between tables that have identity columns? :confused:

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

              PIEBALDconsult wrote:

              Ummmm... so how do you copy records between tables that have identity columns?

              If I have a business need to copy records and one which is not just replication and I didn't have any needs that precluded GUIDs then I would use GUIDs. That however is far different than saying that they should be used for every possible scenario.

              P 1 Reply Last reply
              0
              • J jschell

                PIEBALDconsult wrote:

                Ummmm... so how do you copy records between tables that have identity columns?

                If I have a business need to copy records and one which is not just replication and I didn't have any needs that precluded GUIDs then I would use GUIDs. That however is far different than saying that they should be used for every possible scenario.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #36

                jschell wrote:

                I would use GUIDs.

                OK, me too.

                jschell wrote:

                used for every possible scenario

                I'm pretty sure I didn't say they should.

                J 1 Reply Last reply
                0
                • P PIEBALDconsult

                  jschell wrote:

                  I would use GUIDs.

                  OK, me too.

                  jschell wrote:

                  used for every possible scenario

                  I'm pretty sure I didn't say they should.

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

                  PIEBALDconsult wrote:

                  I'm pretty sure I didn't say they should.

                  You said in another subthread "True, there is never a need" in regards to PK as a sequential integer. I took that to mean that one should use GUID - always (implicit within "never").

                  P 1 Reply Last reply
                  0
                  • J jschell

                    PIEBALDconsult wrote:

                    I'm pretty sure I didn't say they should.

                    You said in another subthread "True, there is never a need" in regards to PK as a sequential integer. I took that to mean that one should use GUID - always (implicit within "never").

                    P Online
                    P Online
                    PIEBALDconsult
                    wrote on last edited by
                    #38

                    jschell wrote:

                    I took that to mean that one should use GUID - always (implicit within "never").

                    No, that's not what I meant, although many seem to take it that way.

                    1 Reply Last reply
                    0
                    • J jschell

                      Mycroft Holmes wrote:

                      I said the PK should NOT be used for anything but the identification of the record.

                      You certainly went on to say that. However before doing that you quoted a specific statement and then said it was wrong. And what you quoted was not wrong.

                      Mycroft Holmes wrote:

                      If ordering is required then add in a specific field to service that requirement, DON'T use the PK field.

                      So you are suggesting that one should add a unique sequential value which proceeds in lockstep with the primary key. And one must ignore EVERY other consideration in making that decision, such as complexity, cost, record size, project size and every other possible consideration. Myself I like to base my designs on the business needs and use best practices to make informed decisions as to what what might be best rather than accepting them as commandments from a supreme being.

                      Mycroft Holmes wrote:

                      There is absolutely nothing arbitrary about these rules.

                      It certainly is. If not quote three authorative references that state exactly that.

                      Mycroft Holmes wrote:

                      Sybase would randomly skip a few 10k counters, some database system would fill in the gaps, some developers want to fill the gaps.

                      And I am rather certain that at one time there was no way to generate a GUID as an intrinsic key in Oracle. But the limitations of a specific database or version has nothing at all to do with anything.

                      P Online
                      P Online
                      PIEBALDconsult
                      wrote on last edited by
                      #39

                      jschell wrote:

                      you quoted a specific statement and then said it was wrong.

                      jschell wrote:
                      One problem with guids is that they have no implicit ordering

                      This is 100% incorrect

                      I took it that he disagreed with the "One problem with" part of the statement, as do I. The "guids ... have no implicit ordering" is correct, but it is a benefit, not a problem.

                      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