Home

Related IF/ELSE IF statements and WHEN clauses in a CASE should not have the same condition

Rule description

  • Related IF/ELSE IF statements and WHEN clauses in a CASE should not have the same condition

 

Non-compliant Code Example

SELECT   ProductNumber, Category =  
      CASE ProductLine  
         WHEN 'R' THEN 'Road'  
         WHEN 'M' THEN 'Mountain'   --Non compliant code (When clauses is having similar condition)
         WHEN 'T' THEN 'Touring'  
         WHEN 'M' THEN 'Sale items'   --Non compliant code (When clauses is having similar condition)
         ELSE 'Not for sale'  
      END,  
   Name  
FROM Production.Product  
ORDER BY ProductNumber;

Compliant Code Example

SELECT   ProductNumber, Category =    --Compliant code (When clauses is having different condition)
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Sale items'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
ORDER BY ProductNumber; 
Visual Expert 2020
 VETSQLRULE18