Last post Jul 09, 2015 12:37 AM by saurabh jain
Jul 09, 2015 12:37 AM|saurabh jain|LINK
About Scope Identity, Identity and Ident Current
SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record
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
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.
To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.
Where two different query window having two different session
In most of our application scenario, we need to get latest inserted row information through SQL query. And for that, we have multiple options like:
All three functions return last-generated identity values. However, the scope and session on which last is defined in each of these functions differ.
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.
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 or same table with generate an identity column, then SCOPE_IDENTITY result is not affected the overline content is incorrect .
“ but if a trigger or a user defined function is affected on the same table that produced the value returns that identity record thenSCOPE_IDENTITY returns
that identity record which is created by trigger or a user defined function.”
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.
I am explaining the above process with the help of some sample query, hope it helps:
Collapse | Copy Code
CREATE TABLE Parent(id int IDENTITY);
CREATE TABLE Child(id int IDENTITY(100,1));
CREATE TRIGGER Parentins ON Parent FOR INSERT
INSERT Child DEFAULT VALUES
--End of trigger definition
SELECT id FROM Parent;
--id is empty.
SELECT id FROM Child;
--ID is empty.
--Do the following in Session 1
INSERT Parent DEFAULT VALUES;
/*Returns the value 100. This was inserted by the trigger.*/
/* Returns the value 1. This was inserted by the
INSERT statement two statements before this query.*/
/* Returns value inserted into Child, that is in the trigger.*/
/* Returns value inserted into Parent.
This was the INSERT statement four statements before this query.*/
-- Do the following in Session 2.
/* Returns NULL because there has been no INSERT action
up to this point in this session.*/
up to this point in this scope in this session.*/
/* Returns the last value inserted into Child.*/