Home

Control structures should use BEGIN...END blocks

Rule description

  • Control structures should use BEGIN...END blocks

 

Non-compliant Code Example

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100   --Non compliant code (Control structures is not beginning with BEGIN...END blocks)
    BEGIN
        Select * From Employee Where id < @Number;
        PRINT 'The number is large.';  
    END;
ELSE   
    BEGIN  
        IF @Number < 10   --Non compliant code (Control structures is not beginning with BEGIN...END blocks)
            BEGIN
                Select * From Employee Where id < @Number;
                PRINT 'The number is small.';  
            END;
        ELSE  
          PRINT 'The number is medium.';  
    END ;  
GO 

Compliant Code Example

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100   --Compliant code (Control structures is beginning with BEGIN...END blocks)
    BEGIN 
        Select * From Employee Where id < @Number;
        PRINT 'The number is large.';  
    END;
ELSE   
   BEGIN  
        IF @Number < 10   --Compliant code (Control structures is beginning with BEGIN...END blocks)
            BEGIN  
                Select * From Employee Where id < @Number;
                PRINT 'The number is small.';  
            END;
        ELSE  
            BEGIN 
                PRINT 'The number is medium.';  
            END;
   END ;  
GO 
Visual Expert 2020
 VETSQLRULE64