scope_identity()

scope_identity() is one of the function in SQL Server. (2000 and above) The usage of this function is to return the primary key of the last record that you insert into the table. Some people also said that you can use @@identity to archive similar result, but there is one problem which is @@identity will return the wrong primary key if there is any use of trigger (that insert another record) during the insert of the record. And scope_identity() only return the primary key from the same table (and same session) of the insertion which prevent user from getting the wrong primary key.

Scope_Identity @ MSDN
Retrieving Scalar Data from a Stored Procedure @ 4guysFromRolla
Check Your SQL Server Identity @ SQL Server Central

Leave a Reply