SQL Getting Last Inserted Id using SCOPE IDENTITY

  • by
sql return last inserted id scope identity

SQL server Database as a function ‘SCOPE_IDENTITY()‘ which can be used to return the last created ID or Identity (Auto Incremented Primary Key) by the SQL query statement. This cannot be used in Update statement queries as it returns only last created Identity by the SQL query statement. sql return last inserted id scope identity.

Syntax for Scope_Identity() usage:

Insert into TableName (ColumnName1,ColumnName2) values (@ColumnName1,@ColumnName2);
Select SCOPE_IDENTITY() as 'Id'

Usage : 

Insert into Producttbl (ProName, ProDesc, OnDate) values
('Samsung Galaxy S9', 'Samsung Flagship Smartphone for 2018', '2018-01-15');
Select SCOPE_IDENTITY() as 'Id'
Select * from Producttbl

Output :

sql return last inserted id scope identity

 

 

Also see : SQL Server – Stored Procedure with Output return Parameter value.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.