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.
  • J jschell

    I figured that might be that persons point. One problem with guids is that they have no implicit ordering. And some times ordering for implementation needs rather than business needs is a good idea. Not to mention size and convenience. And of course guids are likely unique but not guaranteed to be so.

    I Offline
    I Offline
    Igor Jas
    wrote on last edited by
    #9

    From my personal experience guids may affect the performance significantly if they are used for primary key and are randomly generated. To solve this i've read somewhere that is recommended to use NEWSEQUENTIALID() as default, instead NEWID() to generate keys and with clustered indexes the performances will be much better. However if I use these keys as foreign key in other table this will not help as they will not be ordered, so I choose to use integers for primary key. The idea is MSSQL to generate new id and to return it to object in code, but also I've read there might be some problems with using @@IDENTITY or SCOPE_IDENTITY()

    M J 2 Replies Last reply
    0
    • J jschell

      I figured that might be that persons point. One problem with guids is that they have no implicit ordering. And some times ordering for implementation needs rather than business needs is a good idea. Not to mention size and convenience. And of course guids are likely unique but not guaranteed to be so.

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

      jschell wrote:

      One problem with guids is that they have no implicit ordering

      This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

      Never underestimate the power of human stupidity RAH

      P J 2 Replies Last reply
      0
      • I Igor Jas

        From my personal experience guids may affect the performance significantly if they are used for primary key and are randomly generated. To solve this i've read somewhere that is recommended to use NEWSEQUENTIALID() as default, instead NEWID() to generate keys and with clustered indexes the performances will be much better. However if I use these keys as foreign key in other table this will not help as they will not be ordered, so I choose to use integers for primary key. The idea is MSSQL to generate new id and to return it to object in code, but also I've read there might be some problems with using @@IDENTITY or SCOPE_IDENTITY()

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

        Igor Jas wrote:

        I've read there might be some problems with using @@IDENTITY or SCOPE_IDENTITY()

        Reference required, a shot like that requires a reference so we can investigate the veracity of the statement.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • M Mycroft Holmes

          If you start on about guids again I'll scream.

          Never underestimate the power of human stupidity RAH

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

          Nope nope, not this time, nuh uh. But there are better ways of generating integer Ids.

          1 Reply Last reply
          0
          • M Mycroft Holmes

            jschell wrote:

            One problem with guids is that they have no implicit ordering

            This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

            Never underestimate the power of human stupidity RAH

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

            That is one area where you and I seem to agree completely. :jig:

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              That is one area where you and I seem to agree completely. :jig:

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

              PIEBALDconsult wrote:

              agree completely

              Yeah we have the fundamentals right, just disagree on the details but the thats where the art comes in and art is such a personal thing.

              Never underestimate the power of human stupidity RAH

              L 1 Reply Last reply
              0
              • M Mycroft Holmes

                PIEBALDconsult wrote:

                agree completely

                Yeah we have the fundamentals right, just disagree on the details but the thats where the art comes in and art is such a personal thing.

                Never underestimate the power of human stupidity RAH

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

                Guys, let us agree to disagree...

                1 Reply Last reply
                0
                • I Igor Jas

                  I've been searching the net for this but I couldn't find concrete answer what to use to recover identity ID from INSERT stored procedure. What is the correct way to get the ID as output in stored procedure (should I use @@IDENTITY, SCOPE_IDENTITY() or something else). CREATE TABLE [dbo].[tbl_Table]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL ) Thanks,

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

                  go for @@IDENTITY .

                  M 1 Reply Last reply
                  0
                  • I Igor Jas

                    From my personal experience guids may affect the performance significantly if they are used for primary key and are randomly generated. To solve this i've read somewhere that is recommended to use NEWSEQUENTIALID() as default, instead NEWID() to generate keys and with clustered indexes the performances will be much better. However if I use these keys as foreign key in other table this will not help as they will not be ordered, so I choose to use integers for primary key. The idea is MSSQL to generate new id and to return it to object in code, but also I've read there might be some problems with using @@IDENTITY or SCOPE_IDENTITY()

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

                    Igor Jas wrote:

                    However if I use these keys as foreign key in other table this will not help as they will not be ordered

                    That statement is either incomplete or wrong. You might want an implementation (not business way) to order data and as such you could use a integer key to do so. But whether it is a foreign key or not has nothing to do with that.

                    1 Reply Last reply
                    0
                    • M Mycroft Holmes

                      jschell wrote:

                      One problem with guids is that they have no implicit ordering

                      This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

                      Never underestimate the power of human stupidity RAH

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

                      Mycroft Holmes wrote:

                      This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

                      Err...obviously what you quoted is 100% correct. Guids do not have an ordering. Integer values do. That said you might disagree with my assertion that one might need an implicit ordering in a table but that has NOTHING do with what you quoted. Presumably you are claiming that the need is never possible and can never occur. I am claiming that sometimes, rarely, it does provide a solution.

                      M P 2 Replies Last reply
                      0
                      • J jschell

                        Mycroft Holmes wrote:

                        This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

                        Err...obviously what you quoted is 100% correct. Guids do not have an ordering. Integer values do. That said you might disagree with my assertion that one might need an implicit ordering in a table but that has NOTHING do with what you quoted. Presumably you are claiming that the need is never possible and can never occur. I am claiming that sometimes, rarely, it does provide a solution.

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

                        jschell wrote:

                        Err...obviously what you quoted is 100% correct.

                        No you read correctly, this is a fundamental rule of database design and if you ignore it you will get bitten somewhere down the track. A PK has only 1 job, identify the record. Ordering the data is NOT part of the PKs job, thats up to the designer to implement a specific solution. This is one of the strongest arguments FOR guids, it stops developers relying on the PK for anthing but record identification, they can't use them for ordering or to identify part of the the record (the old concatenated string from bits of the record trick). The reson I don't like guids is I can never remember the bloody things when debugging a procedure X| .

                        Never underestimate the power of human stupidity RAH

                        P J 2 Replies Last reply
                        0
                        • P Prasanta_Prince

                          go for @@IDENTITY .

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

                          Only if you don't use functions or nested procedures or triggers. Scope_Identity() is a more robust solution. Learn to use that instead (reteach the fingers).

                          Never underestimate the power of human stupidity RAH

                          P 1 Reply Last reply
                          0
                          • J jschell

                            Mycroft Holmes wrote:

                            This is 100% incorrect, and one of the arguments FOR guids. Primary key should have absolutely NO intelligence other than to identify the record.

                            Err...obviously what you quoted is 100% correct. Guids do not have an ordering. Integer values do. That said you might disagree with my assertion that one might need an implicit ordering in a table but that has NOTHING do with what you quoted. Presumably you are claiming that the need is never possible and can never occur. I am claiming that sometimes, rarely, it does provide a solution.

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

                            jschell wrote:

                            you are claiming that the need is never possible

                            True, there is never a need -- and the desire should be stamped out.

                            jschell wrote:

                            it does provide a solution

                            That's the lazy way out. There is likely one or more other fields you should use -- like a record creation timestamp or something, if you want to order records according to the order in which they were created.

                            J 1 Reply Last reply
                            0
                            • M Mycroft Holmes

                              jschell wrote:

                              Err...obviously what you quoted is 100% correct.

                              No you read correctly, this is a fundamental rule of database design and if you ignore it you will get bitten somewhere down the track. A PK has only 1 job, identify the record. Ordering the data is NOT part of the PKs job, thats up to the designer to implement a specific solution. This is one of the strongest arguments FOR guids, it stops developers relying on the PK for anthing but record identification, they can't use them for ordering or to identify part of the the record (the old concatenated string from bits of the record trick). The reson I don't like guids is I can never remember the bloody things when debugging a procedure X| .

                              Never underestimate the power of human stupidity RAH

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

                              Hear hear!

                              Mycroft Holmes wrote:

                              I can never remember the bloody things

                              I use copy-and-paste. I've also mentioned before that you don't have to use auto-generated GUIDs; you can cobble them up yourself for development and testing purposes.

                              1 Reply Last reply
                              0
                              • M Mycroft Holmes

                                Only if you don't use functions or nested procedures or triggers. Scope_Identity() is a more robust solution. Learn to use that instead (reteach the fingers).

                                Never underestimate the power of human stupidity RAH

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

                                Yes, but better to get the ID one way or another before inserting the record -- as we did with sequences in Oracle. Identity columns are the devil's work. At work recently I tried to copy some records from one table to another, but couldn't because the IDs wouldn't be the same. X| A thousand deaths upon the idiot who created identity/auto-increment columns. :mad:

                                J 1 Reply Last reply
                                0
                                • M Mycroft Holmes

                                  jschell wrote:

                                  Err...obviously what you quoted is 100% correct.

                                  No you read correctly, this is a fundamental rule of database design and if you ignore it you will get bitten somewhere down the track. A PK has only 1 job, identify the record. Ordering the data is NOT part of the PKs job, thats up to the designer to implement a specific solution. This is one of the strongest arguments FOR guids, it stops developers relying on the PK for anthing but record identification, they can't use them for ordering or to identify part of the the record (the old concatenated string from bits of the record trick). The reson I don't like guids is I can never remember the bloody things when debugging a procedure X| .

                                  Never underestimate the power of human stupidity RAH

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

                                  Mycroft Holmes wrote:

                                  No you read correctly

                                  Nope. What you quoted had nothing to do with what you then went on to state was wrong.

                                  Mycroft Holmes wrote:

                                  No you read correctly, this is a fundamental rule of database design and if you ignore it you will get bitten somewhere down the track. A PK has only 1 job, identify the record. Ordering the data is NOT part of the PKs job, thats up to the designer to implement a specific solution.

                                  And as I said sometimes ordering is required. As you said as well. From that one then must decide on a way to provide that ordering. And PK integer might be used for that that. Or not. The needs of the application, business, project needs, etc define the design. Not arbitrary rules.

                                  M 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    jschell wrote:

                                    you are claiming that the need is never possible

                                    True, there is never a need -- and the desire should be stamped out.

                                    jschell wrote:

                                    it does provide a solution

                                    That's the lazy way out. There is likely one or more other fields you should use -- like a record creation timestamp or something, if you want to order records according to the order in which they were created.

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

                                    PIEBALDconsult wrote:

                                    That's the lazy way out. There is likely one or more other fields you should use -- like a record creation timestamp or something, if you want to order records according to the order in which they were created.

                                    And how would you suggest doing it if you want to uniquely identify records using an a value that allows for a range queries in a database that deals with multiple clients?

                                    P 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      Yes, but better to get the ID one way or another before inserting the record -- as we did with sequences in Oracle. Identity columns are the devil's work. At work recently I tried to copy some records from one table to another, but couldn't because the IDs wouldn't be the same. X| A thousand deaths upon the idiot who created identity/auto-increment columns. :mad:

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

                                      PIEBALDconsult wrote:

                                      At work recently I tried to copy some records from one table to another, but couldn't because the IDs wouldn't be the same

                                      I worked with a company that insisted their product worked on Oracle. The schema they delivered obviously originated from MS SQL Server and used quoted identifiers for every single identifier. (My guess - they used a tool to 'convert' the schema prior to delivery to us.) Yet despite that I neither condemn Oracle nor MS SQL Server. I do however blame the company for making claims that obviously they had not designed for. I can only wonder, was the table that you had difficulty with designed to be copied from one location to another while maintaining foreign keys (which I can only presume was the actual problem with ids.) If it wasn't are you suggesting that one should create designs that meet all possible needs of the business of the future? Myself I know that I have yet to meet any developer that is even close to being able to tell the future and the alternative, designing for every possible need, is neither possible nor cost effective. So which of those alternatives are you suggesting business developers should rely on?

                                      P 1 Reply Last reply
                                      0
                                      • J jschell

                                        Mycroft Holmes wrote:

                                        No you read correctly

                                        Nope. What you quoted had nothing to do with what you then went on to state was wrong.

                                        Mycroft Holmes wrote:

                                        No you read correctly, this is a fundamental rule of database design and if you ignore it you will get bitten somewhere down the track. A PK has only 1 job, identify the record. Ordering the data is NOT part of the PKs job, thats up to the designer to implement a specific solution.

                                        And as I said sometimes ordering is required. As you said as well. From that one then must decide on a way to provide that ordering. And PK integer might be used for that that. Or not. The needs of the application, business, project needs, etc define the design. Not arbitrary rules.

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

                                        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 1 Reply Last reply
                                        0
                                        • J jschell

                                          PIEBALDconsult wrote:

                                          At work recently I tried to copy some records from one table to another, but couldn't because the IDs wouldn't be the same

                                          I worked with a company that insisted their product worked on Oracle. The schema they delivered obviously originated from MS SQL Server and used quoted identifiers for every single identifier. (My guess - they used a tool to 'convert' the schema prior to delivery to us.) Yet despite that I neither condemn Oracle nor MS SQL Server. I do however blame the company for making claims that obviously they had not designed for. I can only wonder, was the table that you had difficulty with designed to be copied from one location to another while maintaining foreign keys (which I can only presume was the actual problem with ids.) If it wasn't are you suggesting that one should create designs that meet all possible needs of the business of the future? Myself I know that I have yet to meet any developer that is even close to being able to tell the future and the alternative, designing for every possible need, is neither possible nor cost effective. So which of those alternatives are you suggesting business developers should rely on?

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

                                          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 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