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 get an ID of the last inserted row of a table

How to get an ID of the last inserted row of a table

Scheduled Pinned Locked Moved Database
visual-studiotutorial
7 Posts 4 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.
  • M Offline
    M Offline
    makumazan84
    wrote on last edited by
    #1

    I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.

    B N 2 Replies Last reply
    0
    • M makumazan84

      I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      This query select top 1 id from tablename order by id desc will not be choice for you?


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      M A 2 Replies Last reply
      0
      • B Blue_Boy

        This query select top 1 id from tablename order by id desc will not be choice for you?


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        M Offline
        M Offline
        makumazan84
        wrote on last edited by
        #3

        yeah, it worked. Thank you very much. Still, I have a question: isn't it the same as choosing the maximum ID?

        B 1 Reply Last reply
        0
        • M makumazan84

          yeah, it worked. Thank you very much. Still, I have a question: isn't it the same as choosing the maximum ID?

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          makumazan84 wrote:

          Thank you very much.

          No problem. If ID value is increase-able always then we can use max function to get maximum value of ID. example: select max(id) from tablename


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          1 Reply Last reply
          0
          • B Blue_Boy

            This query select top 1 id from tablename order by id desc will not be choice for you?


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            This only works in a single user environment - otherwise someone else could insert before you run the select. Also, this relies on id being the prmary key, otherwise you should select the max(id) - bad practice to rely on any ordering unless specified. Do't know what the answer is though, I nvere use tableadaptors etc, there isn't enough control over transactions etc for the sort of systems I usually work on.

            Bob Ashfield Consultants Ltd

            B 1 Reply Last reply
            0
            • A Ashfield

              This only works in a single user environment - otherwise someone else could insert before you run the select. Also, this relies on id being the prmary key, otherwise you should select the max(id) - bad practice to rely on any ordering unless specified. Do't know what the answer is though, I nvere use tableadaptors etc, there isn't enough control over transactions etc for the sort of systems I usually work on.

              Bob Ashfield Consultants Ltd

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              Thanks for your information.


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              1 Reply Last reply
              0
              • M makumazan84

                I need to know the ID of a last inserted record. The problems is, that I use VS generated dataset and table adapter, so the classic "SELECT @@IDENTITY" is not working. I also tried to use "SELECT IDENT_CURRENT('tablename')", but for some reasons it crashes at the runtime, saying that function IDENT_CURRENT is not recognized, or something like this.

                N Offline
                N Offline
                nelsonpaixao
                wrote on last edited by
                #7

                select max(id) from mytable should be enought but...you may need to use a lock. Read about it i don´t know much about it! If it´s a read/report procedure (you don´t delete or edit anything) there´s no problem using select max(id )from mytable, otherwise you must use somekind of lock!!! To prevent users to edit same thing same time X|

                nelsonpaixao@yahoo.com.br trying to help & get help

                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