Skip to content

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.

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:

-- Example of problematic query
SELECT * 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.

Separate each T-SQL statement onto its own line to improve readability and maintainability.

Follow these steps to address the issue:

1.Identify lines containing multiple T-SQL statements separated by semicolons.

2.Place each statement on its own line by splitting the combined line into separate lines.

3.Review the newly formatted code to ensure that it maintains the correct logical sequence and clarity.

For example:

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

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

Rule has no parameters.

The rule does not need Analysis Context or SQL Connection.

2 minutes per issue.

Design Rules, Code Smells

There is no additional info for this rule.

CREATE TABLE Test.Greeting
(
GreetingId INT IDENTITY (1,1) PRIMARY KEY,
Message nvarchar(255) NOT NULL,
)
INSERT INTO Test.Greeting (Message) SELECT 'Hello!' UNION ALL SELECT 'Hi!' UNION ALL SELECT 'Hello, world!'
INSERT INTO Test.Greeting (Message)
VALUES ('How do yo do?'),
('Good morning!'),
('Good night!') SELECT * FROM Test.Greeting g
WHERE
g.Message like 'Hello%'
DELETE Test.Greeting WHERE GreetingId = 3; DROP TABLE Test.Greeting
  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

Analysis Rules