rspec/rules/S4801/rule.adoc

40 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
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.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +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.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
As a consequence, the ``++SELECT++`` statement of an ``++EXISTS++`` statement should always contain a ``++WHERE++`` clause.
2020-06-30 12:49:37 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
What is true for ``++EXISTS++`` is also true for ``++NOT EXISTS++``.
2020-06-30 12:49:37 +02:00
=== Noncompliant code example
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
SELECT *
FROM sys.[databases] AS [sd]
WHERE EXISTS (SELECT 1
FROM [sys].[master_files] AS [mf])
----
=== Compliant solution
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
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-06-30 12:49:37 +02:00
2021-01-27 13:42:22 +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