Results 1 to 5 of 5

Thread: @@IDENTITY doesn't work

  1. #1
    Join Date
    Jul 2004
    Posts
    106

    @@IDENTITY doesn't work

    I am trying to get the last ID auto in MS SQL 2000

    SELECT @@IDENTITY as ident FROM users

    and i get nothing

    i am using

    SELECT MAX(id) as ident from users

    what is the correct way to be shure to get at once the real last ID ?

    thank you

  2. #2
    Join Date
    Feb 2003
    Posts
    1,048
    Okay, a word about fetching identity values. There are three basic ways to do it.

    "Select @@IDENTITY" returns the last identity value created by the current session in any scope.

    "Select SCOPE_IDENTITY( )" returns the last identity value created by the current session in the current scope.

    "Select IDENT_CURRENT('table_name')" returns the last identity value for the specified table from any session and any scope.

  3. #3
    Join Date
    Jul 2004
    Posts
    106
    then if i have juste inserted a new row what is the best way to get the new id created ?

    thank you

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    select scope_identity() is safest, but if you are retrieving the value out of its scope then it will not return anything. In that case you have use @@identity.

    Since you are getting value immediately after insert, scope_identity() will work perfectly for you.

  5. #5
    Join Date
    Jul 2004
    Posts
    106
    I'll try scope_identity
    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •