9

Possible Duplicate:
Best way to get identity of inserted row?

When is it appropriate and how should someone use the different IDENTITY key word in T-SQL?

  • SELECT @@IDENTITY,
  • SELECT SCOPE_IDENTITY()
  • SELECT IDENT_CURRENT(‘tablename’)
Community
  • 1
  • 1
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

2 Answers2

5

Have a look at SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record

From the article

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. @@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

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.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • sir, in what case we may face need to access the identity column value? Practically can you give me an example? – NoviceToDotNet Dec 09 '10 at 10:24
  • 1
    If you wish to use the Identity value as a Foreign Key, or to use it in your application to load after you have inserted the new object. – Adriaan Stander Dec 09 '10 at 10:26
  • And don't assume that the value returned is correct: http://support.microsoft.com/kb/2019779 Another workaround is to use an OUTPUT clause on your INSERT statement to capture the new identity value(s). – HABO Dec 23 '11 at 04:06
2

MSDN has a good reference on this.

In a nutshell, @@IDENTITY's scope is the current session on the server (you could end up retrieving the wrong IDENTITY value after an INSERT if e.g. there is a trigger on the table that also adds to a table with an IDENTITY column).

SCOPE_IDENTITY() will return the last inserted ID for the current scope, i.e. will not give you the problem as outlined above.

IDENT_CURRENT is not limited to any session, it returns info at the table-level (the last id generated for that table across any session)

AdaTheDev
  • 142,592
  • 28
  • 206
  • 200