How am I supposed to get the IDENTITY
of an inserted row?
I know about @@IDENTITY
and IDENT_CURRENT
and SCOPE_IDENTITY
, but don't understand the implications or impacts attached to each.
Can someone please explain the differences and when I would be using each?
From MSDN
@@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions in that they return the last value inserted into the IDENTITY column of a table.
@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT.
- IDENT_CURRENT is a function which takes a table as a argument.
- @@IDENTITY may return confusing result when you have an trigger on the table
- SCOPE_IDENTITY is your hero most of the time.