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. General Programming
  3. Visual Basic
  4. Get Last ID from a Table

Get Last ID from a Table

Scheduled Pinned Locked Moved Visual Basic
database
6 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
    Muhammad Fahim Baloch
    wrote on last edited by
    #1

    hi to all, I want to get last inserted id from a particular table, how i write the sql query.....

    D S 2 Replies Last reply
    0
    • M Muhammad Fahim Baloch

      hi to all, I want to get last inserted id from a particular table, how i write the sql query.....

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Normally, you wouldn't do this at all. You let the database keep track of and assign the ID itself when you create a record. Why? Well, say you have 2 clients accessing the database at the same time. They both execute that little function that returns the last ID used and they both get the same ID number. Then, they both generate the next logical ID number and try to use it. Boom. You either just corrupted your data because you don't have any constraints on the key column or you just got an exception thrown back in your face about using the same key more than one in a table.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      S 1 Reply Last reply
      0
      • M Muhammad Fahim Baloch

        hi to all, I want to get last inserted id from a particular table, how i write the sql query.....

        S Offline
        S Offline
        Smithers Jones
        wrote on last edited by
        #3

        What has that to do with VB.Net?

        SELECT @@IDENTITY

        returns the last inserted primary key.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Normally, you wouldn't do this at all. You let the database keep track of and assign the ID itself when you create a record. Why? Well, say you have 2 clients accessing the database at the same time. They both execute that little function that returns the last ID used and they both get the same ID number. Then, they both generate the next logical ID number and try to use it. Boom. You either just corrupted your data because you don't have any constraints on the key column or you just got an exception thrown back in your face about using the same key more than one in a table.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          S Offline
          S Offline
          Samir Ibrahim 0
          wrote on last edited by
          #4

          Although you have 3 remarkable haphazard in your example, but they are acceptable :) Every table should have unique key (PK), that key is usually an Autoinc field type, when insert new record, the table will automatically generate the next ID to be used for the next client. But very often, you need to make your own sequence number need to be written in the table, and you can usually do that by creating a table (numbering) which store in it the next ID for (let say new invoice) and you can luck this record and prevent any one from accessing this record and the user get his ID and unlock the row. My point is, you will not let the database generate new ID for you in every situation.

          Samir R. Ibrahim

          modified on Friday, February 13, 2009 7:50 AM

          D 1 Reply Last reply
          0
          • S Samir Ibrahim 0

            Although you have 3 remarkable haphazard in your example, but they are acceptable :) Every table should have unique key (PK), that key is usually an Autoinc field type, when insert new record, the table will automatically generate the next ID to be used for the next client. But very often, you need to make your own sequence number need to be written in the table, and you can usually do that by creating a table (numbering) which store in it the next ID for (let say new invoice) and you can luck this record and prevent any one from accessing this record and the user get his ID and unlock the row. My point is, you will not let the database generate new ID for you in every situation.

            Samir R. Ibrahim

            modified on Friday, February 13, 2009 7:50 AM

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            True, but locking records manually is VERY problematic if not done with the utmost of care. Most newbies asking questions around here have no idea what they're doing, so using a solution like this is going to be beyond them.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            S 1 Reply Last reply
            0
            • D Dave Kreskowiak

              True, but locking records manually is VERY problematic if not done with the utmost of care. Most newbies asking questions around here have no idea what they're doing, so using a solution like this is going to be beyond them.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              S Offline
              S Offline
              Samir Ibrahim 0
              wrote on last edited by
              #6

              I understood what you want to say. Regards.

              Samir R. Ibrahim

              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