SA0177 : To improve code readability, put only one statement per line

Placing multiple T-SQL statements on the same line can reduce code clarity and increase the risk of errors, making it essential to separate each statement for better readability and maintainability.

Description

Writing more than one SQL statement on a single line can create difficulties in code readability and maintenance. This practice can obscure the logic of the query, making it harder for developers and administrators to identify individual statements, especially when debugging or extending the query.

For example:

SQL
1-- Example of problematic query
2SELECT * FROM Employees; UPDATE Employees SET IsActive = 1 WHERE EmployeeID = 1;

This line includes both a SELECT and an UPDATE statement, which can be confusing for others reviewing the code. Furthermore, it can lead to errors being overlooked, as automated tools and scripts may not always parse combined statements correctly.

  • This approach can reduce code readability, making it tough to understand the individual operations being performed.

  • Combining statements on a single line increases the risk of syntax errors and makes it harder to spot bugs during debugging.

How to fix
Separate each T-SQL statement onto its own line to improve readability and maintainability.Follow these steps to address the issue:Identify lines containing multiple T-SQL statements separated by semicolons.Place each statement on its own line by splitting the combined line into separate lines.Review the newly formatted code to ensure that it maintains the correct logical sequence and clarity.For example:@@_SHFB_2
Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
2 minutes per issue.
Categories

Design Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE Test.Greeting
 2(
 3GreetingId INT IDENTITY (1,1) PRIMARY KEY,
 4Message nvarchar(255) NOT NULL,
 5) 
 6
 7INSERT INTO Test.Greeting (Message) SELECT 'Hello!' UNION ALL SELECT 'Hi!' UNION ALL SELECT 'Hello, world!'
 8
 9INSERT INTO Test.Greeting (Message) 
10VALUES ('How do yo do?'),
11        ('Good morning!'),
12        ('Good night!')  SELECT * FROM Test.Greeting g 
13WHERE 
14g.Message like 'Hello%'
15
16DELETE  Test.Greeting WHERE GreetingId = 3;  DROP TABLE Test.Greeting

Analysis Results
  Message Line Column
1 SA0177 : To improve code readability, put only one statement per line. 16 0
2 SA0177 : To improve code readability, put only one statement per line. 16 45
See Also

Other Resources