0

I have look high and low for a good answer but every answer I have found has been kind of vague in the answer. So what is really better and why for SQL IDs @@IDENTITY or SCOPE_IDENTITY, or what times is is better to use one rather than the other.

Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
jnics23
  • 29
  • 7
  • @SonerGönül strictly not really a duplicate but something like _your answer is here_ however I concur that this should be closed. – Steve May 08 '14 at 13:01

1 Answers1

1

Compare

@@IDENTITY

It returns the last identity value generated for any table in the current session, across all scopes.

Let me explain this... suppose we create an insert trigger on table which inserts a row in another table with generate an identity column, then @@IDENTITY returns that identity record which is created by trigger.

SCOPE_IDENTITY

It returns the last identity value generated for any table in the current session and the current scope.

Let me explain this... suppose we create an insert trigger on table which inserts a row in another table with generate an identity column, then SCOPE_IDENTITY result is not affected but if a trigger or a user defined function is affected on the same table that produced the value returns that identity record then SCOPE_IDENTITY returns that identity record which is created by trigger or a user defined function.

IDENT_CURRENT

It returns the last identity value generated for a specific table in any session and any scope.

In other words, we can say it is not affected by scope and session, it only depends on a particular table and returns that table related identity value which is generated in any session or scope.

Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77