According to MSDN, @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
Has anyone come across a situation where this functionality was useful? I can’t think of a situation when you would want the last ID generated for any table across all scopes or how you would use it.
UPDATE:
Not sure what all the downvotes are about, but I figured I’d try to clarify what I’m asking.
First off I know when to use SCOPE_IDENTITY and IDENT_CURRENT. What I’m wondering is when would it be better to use @@IDENTITY as opposed to these other options? I have yet to find a place to use it in my day to day work and I’m wondering if someone can describe a situation when it is the best option.
Most of the time when I see it, it is because someone doesn’t understand what they were doing, but I assume Microsoft included it for a reason.
In general, it shouldn’t be used.
SCOPE_IDENTITY()is far safer to use (as long as we’re talking about single-row inserts, as highlighted in a comment above), except in the following scenario, where@@IDENTITYis one approach that can be used (SCOPE_IDENTITY()cannot in this case):@@IDENTITYvalue generated within the trigger will never be changed to reflect a different table (e.g. someone adds logging to the trigger after the insert you were relying on)This is an odd use case, but feasible.
Keep in mind this won’t work if you are inserting multiple rows and need multiple
IDENTITYvalues back. There are other ways, but they also require the option to allow result sets from cursors, and IIRC this option is being deprecated.