Home

Statements should be on separate lines

Description

    The "Statements should be on separate lines" code rule for SQL Server means that each individual statement should be written on its own line. This helps to improve readability and maintainability of code. It also helps to ensure that any changes to the code are easier to identify and modify, as each statement is clearly separated from the other statements. This rule helps to ensure that SQL code is easier to read and understand, and can help to reduce the chances of errors or typos occurring.

Key Benefits

  • Easier to read: Separating statements into separate lines makes code easier to read and understand.
  • Easier to debug: Separating statements allows for easier debugging, as it is easier to identify the source of an error.
  • Easier to maintain: Separating statements into separate lines makes code easier to maintain, as changes can be made to individual lines without affecting the whole code.

 

Non-compliant Code Example

DECLARE @Number int;  SET @Number = 50;    --Non compliant code (Declare and Set statements are in the same line)
IF @Number > 100  AND @Number < 200     
    PRINT 'The number is large.';  
ELSE BEGIN IF @Number < 10     --Non compliant code (Else, Begin and If statements are in the same line)
    PRINT 'The number is small.';  
ELSE  
    PRINT 'The number is medium.';  
END ;  
GO 

Compliant Code Example

DECLARE @Number int;   --Compliant code
SET @Number = 50;  
IF @Number > 100  AND @Number < 200     
    PRINT 'The number is large.';  
ELSE 
    BEGIN
        IF @Number < 10    --Compliant code
            PRINT 'The number is small.';  
        ELSE  
            PRINT 'The number is medium.';  
    END ;  
GO 
Visual Expert 2024
 VETSQLRULE73