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. select last insert ID

select last insert ID

Scheduled Pinned Locked Moved Database
databasemysqlsql-serversysadmintutorial
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.
  • B Offline
    B Offline
    briogene
    wrote on last edited by
    #1

    Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene

    A C 2 Replies Last reply
    0
    • B briogene

      Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene

      A Offline
      A Offline
      Aavesh Agarwal
      wrote on last edited by
      #2

      with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh

      B P 2 Replies Last reply
      0
      • A Aavesh Agarwal

        with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh

        B Offline
        B Offline
        briogene
        wrote on last edited by
        #3

        thanks for this avesh it works briogene

        A 1 Reply Last reply
        0
        • B briogene

          Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene

          C Offline
          C Offline
          Chetan Patel
          wrote on last edited by
          #4

          Select scope_identity() -- modified at 5:31 Monday 22nd October, 2007

          Best Regards, Chetan Patel

          1 Reply Last reply
          0
          • B briogene

            thanks for this avesh it works briogene

            A Offline
            A Offline
            Aavesh Agarwal
            wrote on last edited by
            #5

            Your Welcome... Regards Avesh

            1 Reply Last reply
            0
            • A Aavesh Agarwal

              with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You really shouldn't do either of these, as they are both pretty bad practice. If you do a select on @@identity, you get the last inserted identity column regardless of scope. So, suppose you insert a record into your table and there is a trigger behind there that inserts into another table with an identity column on it, @@identity returns the id of the trigger insert and not the main table. SELECT max(id) only works if there aren't multiple people posting into your tables at the same time. The simple solution is to use:

              SELECT @ID = SCOPE_IDENTITY()
              

              This returns the identity of the inserted item on the main table, even if you have lots and lots of trigger inserts going on.

              Deja View - the feeling that you've seen this post before.

              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