DF008: The @@IDENTITY function is used.
Last modified: May 28, 2025
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
Want to find out more?
Overview
Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All features
Get acquainted with the rich features and capabilities of the tool in less than 5 minutes.
Request a demo
If you consider employing this tool for your business, request a demo to see it in action.