Home
Multiline blocks should be enclosed in BEGIN...END blocks
Description
The Multiline blocks should be enclosed in BEGIN...END blocks rule states that any SQL Server code containing multiple lines of code should be enclosed with a BEGIN...END block. This is to ensure that all lines of code are executed together as a single unit and that any errors that occur are handled in the same way. It also helps to make code easier to read and debug.
Key Benefits
- Ensures code readability: BEGIN...END blocks make it easier to read and understand code by separating distinct blocks of code.
- Prevents errors: Having BEGIN...END blocks helps to prevent errors, as it ensures that code within the block is executed as intended.
- Improves performance: Using BEGIN...END blocks can improve the performance of code, as it allows the code to be executed more efficiently.
Non-compliant Code Example
DECLARE @Number int;
SET @Number = 50;
IF @Number > 100 --Non compliant code (Multi-line blocks are not enclosed in the BEGIN ... END blocks.)
PRINT 'The number is large.';
ELSE IF @Number < 10
PRINT 'The number is small.';
ELSE
PRINT 'The number is medium.';
GO
Compliant Code Example
DECLARE @Number int;
SET @Number = 50;
IF @Number > 100 --Compliant code (Multi-line blocks are enclosed in the BEGIN ... END blocks.)
BEGIN
PRINT 'The number is large.';
END
ELSE IF @Number < 10
BEGIN
PRINT 'The number is small.';
END
ELSE
BEGIN
PRINT 'The number is medium.';
END
GO