SA0261 : The number of characters per line should not exceed the configured value

Ensuring SQL code readability by managing line length.

Description

SQL queries that have overly long lines can be difficult to read and maintain. This impacts code readability and makes collaboration among developers more challenging. Ensuring that SQL code is structured with reasonable line lengths enhances clarity and maintainability.

For example:

SQL
1-- Example of problematic query with excessive line length
2SELECT Column1, Column2, Column3, Column4 FROM AReallyLongTableName WHERE Condition1 = 'Value1' AND Condition2 = 'Value2' AND Condition3 = 'Value3';

This query lacks line breaks, making it difficult to visually parse and understand, especially for complex queries or large codebases. Proper formatting is crucial for maintaining SQL scripts.

  • Reduces readability and makes code reviews more cumbersome.

  • Increases the likelihood of errors when modifying or updating the SQL code.

How to fix

Improve SQL code readability by managing line lengths through strategic line breaks.

Follow these steps to address the issue:

  1. Identify SQL statements that exceed the specified maximum line length. Review your code to find long lines of code that are difficult to read.

  2. Insert line breaks at logical operators or clauses to divide the query into manageable sections. For example, after SELECT or WHERE clauses.

  3. Use proper indentation for the subsequent lines following a line break to clearly indicate continuation. This enhances readability and structure.

  4. Review the formatted code to ensure clarity and readability are improved, and that no syntax errors were introduced during the process.

For example:

SQL
1-- Example of corrected query with line breaks for readability
2SELECT Column1, Column2, Column3, Column4 
3FROM AReallyLongTableName 
4WHERE Condition1 = 'Value1'
5  AND Condition2 = 'Value2'
6  AND Condition3 = 'Value3';

Scope

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

Parameters
Name Description Default Value
MaximumLineLength

Maximum number of characters per line.

120

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 SA0261.Greeting 
 2( 
 3    GreetingId INT IDENTITY (1,1) PRIMARY KEY, Message nvarchar(255) NOT NULL, 
 4)
 5
 6INSERT INTO SA0261.Greeting (Message) VALUES ('How do yo do?'), ('Good morning!'), ('Good day!'), ('Good evening!'), ('Good night!')
 7
 8-- this line is not so long comment with more than 100 characters per line.
 9-- this line is a very long and has more than 100 characters, which should trigger a rule violation when the MaximumLineLength parameter has value > 100.
10
11DELETE  SA0261.Greeting WHERE GreetingId = 3

Analysis Results
  Message Line Column
1 The number of characters per line should not exceed the configured value. 6 0
2 The number of characters per line should not exceed the configured value. 9 0
See Also

Other Resources