The
SCOPE_IDENTITY function and the
@@IDENTITY variable return the last identity values that are generated in any table in the current session. However, the
SCOPE_IDENTITY function returns values that are inserted only within the current scope. The
@@IDENTITY variable does not limit the insertion to a specific scope.
For example, assume that the database contains a Table1 and a Table2. Both tables have identity columns. An insert trigger is defined in Table1. When a row is inserted in Table1, the trigger inserts a copy of the row in Table2. This scenario involves the following two scopes:
- Insertion in Table1
- Insertion in Table2 by the trigger
In this scenario, the
@@IDENTITY variable and the
SCOPE_IDENTITY function return different values when a row is inserted in Table1. The
@@IDENTITY variable returns the last identity column value that is inserted across any scopes in the current session. In this case, the
@@IDENTITY variable returns the identity value that is inserted in Table2. However, the
SCOPE_IDENTITY function returns the identity value that is inserted in Table1, because the
SCOPE_IDENTITY function returns the value that is last inserted in the same scope. The
SCOPE_IDENTITY function returns the NULL value if the function is invoked before any INSERT statements that are made to an identity column occur in the scope.
For more information about software update terminology, click the following article number to view the article in the Microsoft Knowledge Base:
824684 Description of the standard terminology that is used to describe Microsoft software updates