SA0012 : Use SCOPE_IDENTITY() instead @@IDENTITY

The topic describes the SA0012 analysis rule.

Message

Use SCOPE_IDENTITY() instead @@IDENTITY

Description

The rule checks for use of @@IDENTITY server variable. It is recommended to use SCOPE_IDENTITY() instead. @@IDENTITY is not limited to a specific scope and is not a reliable indicator of the most recent user-created identity if the column is part of a replication article.

How to fix

Replace @@IDENTITY usages with SCOPE_IDENTITY().

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
3 minutes per issue.
Categories

Design Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
1-- SA0012 analysis rule is voilated here
2SELECT @@IDENTITY
3
4-- OK
5SELECT SCOPE_IDENTITY()

Example Test SQL with Automatic Fix
SQL
1-- SA0012 analysis rule is voilated here
2SELECT SCOPE_IDENTITY()
3
4-- OK
5SELECT SCOPE_IDENTITY()

Analysis Results
  Message Line Column
1 SA0012 : Use SCOPE_IDENTITY() instead @@IDENTITY. 2 7
See Also

Other Resources