rspec/rules/S4801/rule.adoc

40 lines
1.2 KiB
Plaintext

== Why is this an issue?
An "EXISTS" statement is generally used to select/update/delete some rows of a table based on the content of columns of other tables.
If the "SELECT" statement used as argument of the "EXISTS" statement is always returning "true" for all rows of the main ``++SELECT++`` statement, the ``++EXISTS++`` statement is useless and has the same effect as if it was not there. Still, this is probably not the original intend of the developer to have an ``++EXISTS++`` statement that is always true.
As a consequence, the ``++SELECT++`` statement of an ``++EXISTS++`` statement should always contain a ``++WHERE++`` clause.
What is true for ``++EXISTS++`` is also true for ``++NOT EXISTS++``.
=== Noncompliant code example
[source,text]
----
SELECT *
FROM sys.[databases] AS [sd]
WHERE EXISTS (SELECT 1
FROM [sys].[master_files] AS [mf])
----
=== Compliant solution
[source,text]
----
SELECT *
FROM sys.[databases] AS [sd]
WHERE EXISTS (SELECT 1
FROM [sys].[master_files] AS [mf]
WHERE [mf].[database_id] = [sd].[database_id])
----
=== Exceptions
This rule doesn't raise an issue when ``++EXISTS++`` is used in the context of a ``++WHILE++`` or a ``++IF++`` statement.