Scope_Identity() vs @@Identity

Posts   
 
    
Posts: 23
Joined: 13-Oct-2005
# Posted on: 11-Nov-2005 19:03:48   

What are the pros and cons of using Scope_Identity() vs @@Identity? (If it matters, the environment is Self-Serving with SQL Server).

Thanks!

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 12-Nov-2005 05:58:31   

Here's a quick article on it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro01/html/sql01d4.asp. In Summary Scope_Identity() will return the value of the last inserted row within the current scope, important for triggers, while @@Identity returns the value of the last inserted row globally.

pilotboba
User
Posts: 434
Joined: 05-Aug-2005
# Posted on: 14-Nov-2005 15:46:10   

bclubb wrote:

Here's a quick article on it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro01/html/sql01d4.asp. In Summary Scope_Identity() will return the value of the last inserted row within the current scope, important for triggers, while @@Identity returns the value of the last inserted row globally.

That's not exactly right unless you don't mean globally the way the word implies it. From the SQL Books On Line:

_ @@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope. _

BOb

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 14-Nov-2005 18:15:33   

Correct, BOb. simple_smile You should always select Scope_Identity() unless: - you're on SqlServer 7 - you use triggers to insert the identity value (for example your entity is mapped onto a view). In this case, @@Identity is required, because the trigger is another scope.

Frans Bouma | Lead developer LLBLGen Pro