DF110: Conditionally executed code is not enclosed within a BEGIN…END block.

The topic describes the DF110 T-SQL code analysis rule.

Category

STYLE

Message

Conditionally executed code is not enclosed within a BEGIN…END block.

Description

For better readability, it is recommended to enclose the conditionally executed code within a BEGIN…END block.

Additional information

Not enclosing conditionally executed code within a BEGIN…END block can lead to unintended consequences and make the code harder to read and maintain. Without the block, only the first subsequent statement is considered part of the conditional execution, potentially leading to confusion about which statements are actually being executed conditionally. Additionally, enclosing code within a BEGIN…END block enhances clarity and makes it easier to identify the scope of the conditional execution. It also ensures that multiple statements are treated as a single unit, reducing the risk of errors and improving code readability.

Noncompliant code example

IF DATEPART(HOUR, GETDATE()) > 17
  SELECT
    'It is time to go home!'

Compliant solution

IF DATEPART(HOUR, GETDATE()) > 17
BEGIN
  SELECT
    'It is time to go home!'
END