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. retrieve auto number ID

retrieve auto number ID

Scheduled Pinned Locked Moved Database
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.
  • P Offline
    P Offline
    playout
    wrote on last edited by
    #1

    Hi, I am inserting a new record to a SQL 2005 database table(tClients). I have a Primary Key called iClientID in that I use to bind clients, that is Auto Number. I want to retrieve the iClientID once I inserted the record, as part of my stored procedure. Here is my insert statement. INSERT INTO tClients(sClientName, sClientAddress, sClientContact, iClientOrder) VALUES ('Joe Simpson', '3 Green Ave' , 'Mr. R Simpson' , 3)

    C 1 Reply Last reply
    0
    • P playout

      Hi, I am inserting a new record to a SQL 2005 database table(tClients). I have a Primary Key called iClientID in that I use to bind clients, that is Auto Number. I want to retrieve the iClientID once I inserted the record, as part of my stored procedure. Here is my insert statement. INSERT INTO tClients(sClientName, sClientAddress, sClientContact, iClientOrder) VALUES ('Joe Simpson', '3 Green Ave' , 'Mr. R Simpson' , 3)

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      DECLARE @Id INT
      INSERT INTO tClients(sClientName, sClientAddress, sClientContact, iClientOrder)
      VALUES ('Joe Simpson', '3 Green Ave' , 'Mr. R Simpson' , 3)
      SELECT @Id = SCOPE_IDENTITY();


      Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      P 1 Reply Last reply
      0
      • C Colin Angus Mackay

        DECLARE @Id INT
        INSERT INTO tClients(sClientName, sClientAddress, sClientContact, iClientOrder)
        VALUES ('Joe Simpson', '3 Green Ave' , 'Mr. R Simpson' , 3)
        SELECT @Id = SCOPE_IDENTITY();


        Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        P Offline
        P Offline
        playout
        wrote on last edited by
        #3

        Thanks Colin, I just have one more question... What is SCOPE_IDENTITY()?

        C K S 3 Replies Last reply
        0
        • P playout

          Thanks Colin, I just have one more question... What is SCOPE_IDENTITY()?

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          playout wrote:

          I just have one more question... What is SCOPE_IDENTITY()?

          What you asked for. The ID of the most recently inserted row (in this scope).


          Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          1 Reply Last reply
          0
          • P playout

            Thanks Colin, I just have one more question... What is SCOPE_IDENTITY()?

            K Offline
            K Offline
            Krish KP
            wrote on last edited by
            #5

            SCOPE_IDENTITY Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch.

            Regards KP

            1 Reply Last reply
            0
            • P playout

              Thanks Colin, I just have one more question... What is SCOPE_IDENTITY()?

              S Offline
              S Offline
              Sathesh Sakthivel
              wrote on last edited by
              #6

              Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

              Regards, Satips.

              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