In this article I will explain the difference between @@Identity And Scope_Identity().Many times we want the inserted identity value. So there comes two terms @@Identity And Scope_Identity().So to understand these things you should clear these terms first scope and session.
@@Identiy will return identity value in current session but any scope.Scope_Identity() will return identity value in current session in current scope.
Now what are these Scope and Session?
Session means the connection that is currently executing that command.Scope I will Explain with this :Suppose you have a stored procedure for inserting some values in a table. You are calling other stored procedure in that procedure means nested in that .So your outer Proc context is one scope and your nested procedure that you have called is in other scope. Like wise suppose you have written a query for inserting records into a table and you have written trigger on that which will insert record in another table, so in this case your query context is one scope and your trigger context is other scope. Now Consider these
Examples
To explain that I have taken two tables and one trigger.When record is inserted in one table its id value is inserted in second table through that trigger.
Create table Test_Identity(id int identity ,[task] varchar(20) ,assigned_to varchar(20))Goinsert into Test_Identityselect 'task1' , 'bsmith'Union allselect 'task2' , 'csmith'GoCreate table Test_Identity_Second_Table(id int identity ,[Test_Identity_From_First_Table] int )GoCreate trigger _Insert_Test_Trigger_Identityon [Test_Identity]After insertAsBegininsert into Test_Identity_Second_Table([Test_Identity_From_First_Table])Select id from inserted EndGOinsert into Test_Identity([task],[assigned_to])values ('task3' , 'esmith')GOSelect @@identity–Output—————————————1Select Scope_Identity()—————————————3Go
In above you got 1 as returned identity value in current session but any scope .So 1 is the value of latest identity which has been inserted in Test_Identity_Second_Table through trigger and you got 3 for Scope_Identity because At current scope 3 is the value of identity which has been inserted that is in table Test_Identity.
Now second example through stored procs
Make Two Procs. Drop that tables and again create them
Godrop table Test_Identitydrop table Test_Identity_Second_TableGOCreate table Test_Identity(id int identity ,[task] varchar(20) ,assigned_to varchar(20))Goinsert into Test_Identityselect 'task1' , 'bsmith'Union allselect 'task2' , 'csmith'GoCreate table Test_Identity_Second_Table(id int identity ,[Test_Identity_From_First_Table] int )GoCReate proc Insert_Proc_Test_Identity_Second_Table@id intasbegininsert into Test_Identity_Second_Table([Test_Identity_From_First_Table])values (@id)endGOCreate proc Insert_Proc_Test_Identity@task varchar(20),@assigned_to varchar(20)asbegindeclare @id as int insert into Test_Identity([task],[assigned_to])values (@task ,@assigned_to)Set @id=Scope_Identity()exec Insert_Proc_Test_Identity_Second_Table @idSelect Scope_Identity() as [ScopeIdentity]Select @@identity as [Identity]endGo–Test It exec Insert_Proc_Test_Identity 'task3' , 'esmith'--OutputScopeIdentity—————————————3Identity—————————————1
In Above you got 1 as returned identity value in current session but in any scope.So 1 is the value of latest identity which has been inserted in Test_Identity_Second_Table through nested stored procedure Insert_Proc_Test_Identity_Second_Table and you got 3 for Scope_Identity because at current scope 3 is the value of identity which has been inserted,ie in table Test_Identity in current context of Proc Insert_Proc_Test_Identity.
@@Identity is a globa variable used for to get the last generated id globally. While scope_identity used for to get the last generated id in a scope(procedure or function). For more detail see at :- http://interview-preparation-for-you.blogspot.com/2011/02/identity-and-scope-identity.html
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18