Code Analysis

BP010

Usage of @@IDENTITY

@@IDENTITY is being used to get the value of the last identity insertion. The @@IDENTITY function returns the last IDENTITY value created in the same session. If you have a trigger on the table, or if the table is a publication in a replication, then the value might be wrong. The SCOPE_IDENTITY() function returns the last identity created in the same  session and within the same scope and so is safer.

If you have a trigger on the table, the value can sometimes be wrong. The @@IDENTITY function returns the last identity created in the same session whereas SCOPE_IDENTITY() function returns the last identity created in the same scope as well. Using SCOPE_IDENTITY() is safer.

For further details and worked examples, see this Product Learning article:  Why you Should Avoid Using the @@IDENTITY Function.

Available in

SQL Prompt

SQL Code Guard



Didn't find what you were looking for?