@@identity VS. scope_identity()

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 years ago
hi, i looked at your stored procedures and found out that you are using rather @@identity than scope_identtiy() function.

@@IDENTITY will return the last identity value entered into a table in current session. @@IDENTITY is limited to the current session, not to the current scope. for example if you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

SCOPE_IDENTITY() returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value. SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to current scope as well. it will return the last identity value that was explicitly created, rather than any identity that was created by a trigger or a user defined function.

there is a third method to obtain - IDENT_CURRENT(‘tablename’)
returns last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value. IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.  


To avoid potential problems I always use SCOPE_IDENTITY() to return the identity of the recently added row in T SQL Statement or Stored Procedure.
14 years ago
Thanks! We have already replaced all @@IDENTITY with SCOPE_IDENTITY(). It'll be used in the next release
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.