DF008: The @@IDENTITY function is used.
Last modified: December 25, 2024
The topic describes the DF008 T-SQL code analysis rule.
Category
BEST PRACTICE
Message
The @@IDENTITY function is used.
Description
The @@IDENTITY function may return incorrect results. It is recommended to use the SCOPE_IDENTITY() function instead.
Additional information
It is recommended to rewrite the stored procedure being called or Transact-SQL statement to use the SCOPE_IDENTITY() function. This function returns the most recent identity value generated within the scope of the user statement rather than the identity value within any nested trigger used by replication.
Noncompliant code example
INSERT INTO dbo.Customers(FirstName, LastName) VALUES(@FirstName, @LastName)
SELECT @@IDENTITY AS CustomerID
Compliant solution
INSERT INTO dbo.Customers(FirstName, LastName) VALUES(@FirstName, @LastName)
SELECT SCOPE_IDENTITY() AS CustomerID
Was this page helpful?