Thursday, April 6, 2017

How to get last insert identity value in SQL server

In SQL Server, to get the last inserted IDENTITY value generated on a connection @@IDENTITY is used. @@IDENTITY is a built-in function to return the last insert IDENTITY value in any table that produced the value, and regardless of the scope of the statement that produced the value in the current session.

If a trigger is available on the table that causes an identity value to be created on another table, then @@IDENTITY will return the last created identity from the later table not from the former table from where the trigger is fired.

The scope of the @@IDENTITY function is current session on the local server on which it is executed. This function cannot be applied to remote or linked servers. To obtain an identity value on a different server, execute a stored procedure on that remote or linked server and have that stored procedure (which is executing in the context of the remote or linked server) gather the identity value and return it to the calling connection on the local server.


            SELECT @@IDENTITY

It returns the last inserted identity in any table in the current session.

Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.