Pro Programmer: @@identity, scope identity, and ident_current

Wednesday, August 15, 2012

@@identity, scope identity, and ident_current


SELECT @@IDENTITY
It returns the last identity value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.

SELECT SCOPE_IDENTITY () It returns the last identity value produced on a connection and by a statement in the same scope, regardless of the table that produced the value. scope_identity(), like @@identity, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

SELECT IDENT_CURRENT (‘tablename’)
It returns the last identity value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
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.

No comments:

Post a Comment