Skip to main content

Difference between scope_identity (),@@identity, ident_current


Identity is the property in table. Identity column values automatically assigned value whenever new record inserted into a table.

Note: A table having only one identity column.

In real time scenario whenever new record inserted into record we need to return that Last Identity value to end user, because of end user application need to check the record gets inserted successfully or not.

If we want to return the last identity value we have 3 built in statements in SQL SERVER.

1.       scope_identity ()

2.       @@identity

3.       ident_current

Scope_identity ():

This function returns the last identity genarated value in the current session and same currnet scope.

@@identity

This function returns the last identity geanrated value in the current and regardless of scope.

ident_current

In this function we need to pass table name as input parameter. It will return the last identity genarated value in passed table regarless of session.

Example:

--Create Student
if object_id('Student') is null
create table Student(id int  identity(1,1) ,Name varchar(20),Marks int)

In Above Student table is the Id-Identity column.
Open New Query Window :
insert into Student(Name,Marks) values('Rakesh',500)

select scope_identity() as [scope_Identity] 




Open New Query Window :

Run this Query

select scope_identity() as [scope_Identity]

--It will return NULL, Because of Scope_identity () returns the Current Session only. Here Current Session means whenever new window will open new session will create. @@Identity also genarated values based on session and scope.

But there is a small diffrence between scope_identity(),@@Identity

Example:

Please truncate all data from Student

 truncate table Student

-- Create Stored Procdure Sub
create procedure Ins_Stu_Sub
(@Name varchar(100),@Marks int)
as
begin
    
     insert into Student(Name,Marks) values(@Name,@Marks)
   
end

-- Create Stored Procdure Main
create procedure Ins_Stu_Main
(@Name varchar(100),@Marks int)
as
begin
    
     insert into Student(Name,Marks) values(@Name,@Marks)
     exec Ins_Stu_Sub @Name,@Marks
     select scope_identity() as [scope_identity],@@identity as [identity]

end

--Execute Main Proceure with Values
exec Ins_Stu_Main 'Rakesh',500




















Here Scope_identity returns the same session and same scope generated value. Here Scope-1 Means Procedure -1, Scope -2 Means Procedure -2 @@identity returns different scope values also .

ident_current:

select ident_current('Student') as[ident_current]

Here ident_current is accepts table name as input parameter . It does not dependent on session and scope.










Comments

  1. Thanks for sharing Rakesh, ident_current is new for me....

    ReplyDelete

Post a Comment

Popular posts from this blog

Coalesce function

Coalesce function returns the first non-null value among the arguments. Syntax: Coalesce (expression [,..n]) Here is example using Coalesce function Example 1 DECLARE @Str1 varchar ( 10 ), @str2 varchar ( 20 ), @Str3 varchar ( 20 ) SET @Str2 = 'Sql' , @Str3 = 'Server' SELECT COALESCE ( @Str1 , @str2 , @Str3 ) As [Coalesce] In above example @Str2 value is ‘Sql’ , @str3 value is ‘Server’  and @str1 values is Null because it not assigned any value . Output: It return’s “Sql” because Coalesce function return’s first non null value. Example 2: Coalesce in select statement. IF OBJECT_ID ( 'Employee' , 'U' ) IS NOT NULL DROP TABLE Employee CREATE TABLE Employee (   ID INT IDENTITY ( 1 , 1 ) PRIMARY KEY ,   NAME VARCHAR ( 20 ),   SALARY INT ) INSERT INTO Employee   ( NAME , SALARY ) VALUES ( 'Rakesh' , 5000 ),(NULL, 6000 ),( 'Naresh...

Variables in T-SQL

Variables can be used to store the data in temporally based on data type. Variable name begin with @ symbol. There are two types of variables in T-SQL. 1.   Local Variables (It must begin @ symbol). 2.   Global Variables (it must begin @@ symbol) this variables also known as system variables. Syntax Declare a Variable DECLARE   @Variable-Name   DATATYPE EXAMPLE: DECLARE   @Name   VARCHAR ( 100 ) In before 2008 versions of SQL SERVER we declare a variable and assign a value in 2 lines.                 DECLARE   @Name   VARCHAR ( 100 ) SET   @Name = 'LeadFirstSQL' In 2008 and Later versions of SQL SERVER we declare a variable and assign a value in single line. DECLARE   @Name   VARCHAR ( 100 )   = 'LeadFirstSQL' Variables must be declare and used with the same batch. DECLARE   @Name   VARCHAR ( 100 ) ...