SA0145 : The EOL marker sequence is not the expected {CR}{LF}
Introduction
Section titled “Introduction”The improper use of end-of-line (EOL) characters can lead to issues when managing SQL code across different platforms and tools, affecting interpretation and execution.
Description
Section titled “Description”In T-SQL code, consistency in using the correct end-of-line character sequence is crucial for maintaining code compatibility across various systems and editing tools. SQL Server typically expects lines to end with a carriage return followed by a line feed ( {CR}{LF} ). This standard helps ensure that SQL code is interpreted correctly, especially when moved between different environments or when used with various tools and editors.
For example:
-- Query with inconsistent EOL charactersSELECT * FROM Employees -- {LF} used instead of {CR}{LF}Using an incorrect EOL character, like {LF} alone, might lead to parsing errors or unexpected behavior in certain SQL Server environments or tools that rely on the traditional {CR}{LF} sequence. It can also complicate version control and collaboration if team members use different editors with varying EOL defaults.
-
Potential for syntax errors due to incorrect code parsing in some SQL Server contexts.
-
Increased difficulty in maintaining and collaborating on SQL code due to varied behavior across different editors and platforms.
How to fix
Section titled “How to fix”Ensure SQL code compatibility by standardizing the end-of-line characters to the SQL Server expected {CR}{LF} sequence.
Follow these steps to address the issue:
1.Open the SQL file in SQL Server Management Studio (SSMS) or Visual Studio.
2.Navigate to the file menu and select File > Save As... .
3.In the Save File As dialog, look for the Save with Encoding... dropdown button next to the Save button.
4.Select Windows (CR LF) for the file’s line endings from the available options.
5.Save the file to apply the correct end-of-line characters.
For example:
-- Example of corrected EOL characters in a querySELECT * FROM Employees;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”8 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”SELECT *FROM Table1, Table2 b, (SELECT * FROM Table4c) cINNER JOIN Table3 ON Table3.Table4_id = c.idINNER JOIN Table5 -- The line ends with '\r'INNER JOIN Table6 ON Table6.Table5_id = Table5.id -- The line ends with '\n'ON Table3.Table4_id = c.id, Table8 f, -- The line ends with '\r' Table9, (SELECT * FROM Table7) h;Analysis Results
Section titled “Analysis Results”No violations found.