rspec/rules/S4801/rule.adoc

33 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-06-30 12:49:37 +02:00
An "EXISTS" statement is generally used to select/update/delete some rows of a table based on the content of columns of other tables.
2020-12-23 14:59:06 +01:00
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.
2020-06-30 12:49:37 +02:00
2020-12-23 14:59:06 +01:00
What is true for ``EXISTS`` is also true for ``NOT EXISTS``.
2020-06-30 12:49:37 +02:00
== Noncompliant Code Example
----
SELECT *
FROM sys.[databases] AS [sd]
WHERE EXISTS (SELECT 1
FROM [sys].[master_files] AS [mf])
----
== Compliant Solution
----
SELECT *
FROM sys.[databases] AS [sd]
WHERE EXISTS (SELECT 1
FROM [sys].[master_files] AS [mf]
WHERE [mf].[database_id] = [sd].[database_id])
----
== Exceptions
2020-12-23 14:59:06 +01:00
This rule doesn't raise an issue when ``EXISTS`` is used in the context of a ``WHILE`` or a ``IF`` statement.
2020-06-30 12:49:37 +02:00