SCOPE_IDENTITY vs @@IDENTITY

SCOPE_IDENTITY and @@IDENTITY both return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope. A scope is a module: a stored procedure, trigger, function, or batch.

If you have triggers in play, @@IDENTITY may not return the value you expected.

Use SCOPE_IDENTITY to ensure you’re returning the key of the record you just created.

Advertisements