In SQL Server, you can use the T-SQL SCOPE_IDENTITY()
function to return the last identity value inserted into an identity column in the same scope.
A scope is a module (stored procedure, trigger, function, or batch). If two statements are in the same stored procedure, function, or batch, they are in the same scope.
Note that it returns the last identity value generated in any table in the current session. This is in contrast to the IDENT_CURRENT()
function, which returns the last-inserted identity value for a given table, regardless of which session it’s in.
SCOPE_IDENTITY()
is very similar to @@IDENTITY
in that they both return the last-inserted identity value in the current session. The difference is that SCOPE_IDENTITY()
is limited to the current scope, whereas @@IDENTITY
is not limited to a specific scope.