T-SQL Plugin - rule for EXISTS / NOT EXISTS

tsql

(Christian Rohr) #1

When a SQL statement uses an EXISTS / NOT EXISTS then there needs to be a WHERE clause in it as well.

Not compliant:

e.g.

SELECT  * 
FROM    sys.[databases] AS [sd]
WHERE EXISTS (SELECT  1 
              FROM    [sys].[master_files] AS [mf])

Compliant:

e.g.

SELECT  * 
FROM    sys.[databases] AS [sd]
WHERE EXISTS (SELECT  1 
              FROM    [sys].[master_files] AS [mf]
              WHERE [mf].[database_id] = [sd].[database_id])

If the statement would be an update or delete instead of a select there would be to many rows deleted.

Thank you very much.

Regards,

Chris


(Alexandre Gigleux) #3

Hello Chris,

Thanks for the rule suggestion. I created the specification https://jira.sonarsource.com/browse/RSPEC-4801 (I re-used your code samples) and the related implementation tickets to T-SQL and PL/SQL:

Regards