DF008: The @@IDENTITY function is used.

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

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Studio in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Studio for your business, request a demo to see it in action.
Ready to start using dbForge Studio for SQL Server?