rspec/rules/S4108/tsql/rule.adoc

42 lines
901 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
``++@@IDENTITY++`` returns the last identity column value created on a connection, regardless of the scope. That means it could return the last identity value you produced, or it could return a value generated by a user defined function or trigger, possibly one fired because of your insert. In order to access the last identity value created in your scope, use ``++SCOPE_IDENTITY()++`` instead.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
INSERT ...
SET @id = @@IDENTITY -- Noncompliant
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
INSERT ...
SET @id = SCOPE_IDENTITY()
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "SCOPE_IDENTITY()" to be sure you're accessing the last id you created on the connection.
=== Highlighting
``++@@IDENTITY++``
endif::env-github,rspecator-view[]