SQL Server: Why Scope_Identity() vs @@identity

In SQL Server we have 3 methods to get the auto-generated identity:

1.      IDENT_CURRENT(‘tablename’)

2.      @@IDENTITY

3.      SCOPE_IDENTITY()

 

But the methods above have some difference:

SQL statement Limit to specified table Limit to current session Limit to current scope
 IDENT_CURRENT(‘tablename’) Yes No No
@@IDENTITY No Yes No
SCOPE_IDENTITY() No Yes Yes

That’s why we prefer SCOPE_IDENTITY(). 微笑

 

In MSDN there are also some description/discussion on this topic:

  • SCOPE_IDENTITY (Transact-SQL)

    http://msdn.microsoft.com/en-us/library/ms190315.aspx

  • Scope_Identity vs @@identity

    http://social.msdn.microsoft.com/Forums/en/transactsql/thread/77caea32-a8a6-479e-95d8-ffdf32cecd3d



你可能感兴趣的:(sql,sql,server,methods)