Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » System FunctionsRSS Feeds

SCOPE_IDENTITY Function

Posted By: Sarita Patel     Category: SQL Server     Views: 21134

This article explains about SCOPE_IDENTITY function in sql server with examples.

SCOPE_IDENTITY is used to get last identity value inserted into an identity column in the same scope. Scope can be stored procedure, trigger, function or batch.

SCOPE_IDENTITY() will return the IDENTITY value inserted, which was the last INSERT that occurred in the same scope. 

The SCOPE_IDENTITY() function will return the NULL value if the function is invoked before any insert statements into an identity column occur in the scope.



Syntax of SCOPE_IDENTITY() Function :


SCOPE_IDENTITY()

Return type of above function is a sql_variant.




Examples of SCOPE_IDENTITY() Function :

Example 1 : Use of SCOPE_IDENTITY() function in select clause 

INSERT INTO [Northwind].[dbo].[Shippers]
           ([CompanyName]
           ,[Phone])
     VALUES
           ('Load Runner',
            '(503) 555-9830')


SELECT SCOPE_IDENTITY()

Output
4

Above example returns lastly inserted identity values in identity column name ShipperID in the Shippers table in current session.
  
Share: 

 
 

Didn't find what you were looking for? Find more on SCOPE_IDENTITY Function Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of SCOPE_IDENTITY Function is from United States.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!