rspec/rules/S4094/tsql/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

65 lines
1.3 KiB
Plaintext

== Why is this an issue?
Under the covers, Simple ``++CASE++`` expressions are evaluated as searched ``++CASE++`` expressions. That is,
----
CASE @foo
WHEN 1 THEN 'a'
WHEN 2 THEN 'b'
----
is actually evaluated as
----
CASE
WHEN @foo = 1 THEN 'a'
WHEN @foo = 2 THEN 'b'
----
In most situations the difference is inconsequential, but when the input expression isn't fixed, for instance if ``++RAND()++`` is involved, it is likely to yield unexpected results. For that reason, it is better to evaluate the input expression once, assign it to a variable, and use the variable as the ``++CASE++``'s input expression.
This rule raises an issue when any of the following is used in a ``++CASE++`` input expression: ``++RAND++``, ``++NEWID++``, ``++CRYPT_GEN_RANDOM++``.
=== Noncompliant code example
[source,sql]
----
CASE CONVERT(SMALLINT, RAND()*@foo) -- Noncompliant
WHEN 1 THEN 'a'
WHEN 2 THEN 'b'
----
=== Compliant solution
[source,sql]
----
DECLARE @bar SMALLINT = CONVERT(SMALLINT, RAND()*@foo)
CASE @bar
WHEN 1 THEN 'a'
WHEN 2 THEN 'b'
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Assign this "CASE" input expression to a variable so that it's executed only once, and evaluate the variable instead.
=== Highlighting
the case input expression
endif::env-github,rspecator-view[]