Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@@IDENTITY, SCOPE_IDENTITY(), OUTPUT and other methods of retrieving last identity

I have seen various methods used when retrieving the value of a primary key identity field after insert.

declare @t table (     id int identity primary key,     somecol datetime default getdate() ) insert into @t default values  select SCOPE_IDENTITY() --returns 1 select @@IDENTITY --returns 1 

Returning a table of identities following insert:

Create Table #Testing (       id int identity,       somedate datetime default getdate()   )   insert into #Testing   output inserted.*   default values    

What method is proper or better? Is the OUTPUT method scope-safe?

The second code snippet was borrowed from SQL in the Wild

like image 706
Seibar Avatar asked Jan 26 '09 21:01

Seibar


People also ask

What is @@ identity and @scope_identity?

@@IDENTITY returns the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() returns the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope.

How do you retrieve the last identity value that is generated?

We use SCOPE_IDENTITY() function to return the last IDENTITY value in a table under the current scope.

What is @@ identity used for?

The @@IDENTITY is a system function that returns the last IDENTITY value generated for any table with an identity column under the current session, regardless of the scope of the T-SQL statement that generated the value.

How do I get the last identity inserted in SQL Server?

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.


1 Answers

It depends on what you are trying to do...

@@IDENTITY

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. @@IDENTITY is limited to the current session and is not limited to the current scope. For example, 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.

SCOPE_IDENTITY()

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() is similar to @@IDENTITY, but it will also limit the value to your current scope. 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.

IDENT_CURRENT()

Returns the last IDENTITY value produced in a table, regardless of the connection and scope of the statement that produced the value. IDENT_CURRENT is limited to a specified table, but not by connection or scope.

like image 71
mson Avatar answered Sep 18 '22 13:09

mson