SA0235 : Consider using the AS keyword to specify a column alias instead of the column_alias = expression syntax
Introduction
Section titled “Introduction”Using column_alias = expression syntax may reduce readability and does not align with the ANSI SQL standard, which recommends the AS keyword for column aliasing.
Description
Section titled “Description”Using column_alias = expression syntax in T-SQL scripts may lead to confusion and poor readability. Although this syntax is legal in SQL Server, it does not conform to the SQL ANSI standard, which suggests using the AS keyword for aliasing columns.
For example:
-- Example of problematic query using assignment operator for aliasingSELECT column1 = expression1FROM TableName;The example above can be misleading because it resembles variable assignment rather than simple aliasing. It is not easily recognizable to those familiar with standard SQL conventions, making it harder to maintain consistency across different RDBMS systems.
-
Decreases readability and increases potential for misunderstanding among team members who expect standard syntax.
-
May lead to compatibility issues when migrating code to different systems that adhere strictly to ANSI SQL.
How to fix
Section titled “How to fix”Use the AS keyword for column aliasing to improve readability and maintain SQL ANSI standard compliance.
Follow these steps to address the issue:
1.Identify any instances in your T-SQL queries where the column_alias = expression syntax is used.
2.Replace the format column_alias = expression with expression AS column_alias .
3.Ensure that all team members are aware of the change and adhere to using the AS syntax for consistency and readability.
For example:
-- Problematic query using assignment operator for aliasingSELECT column1 = expression1FROM TableName;
-- Corrected query using AS keyword for aliasingSELECT expression1 AS column1FROM TableName;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”2 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”-- OKSELECT au_id+au_id AS alias_for_colFROM dbo.authors
-- OKSELECT au_id+au_id AS [alias_for_col]FROM dbo.authors
-- OKSELECT au_id+au_id AS "alias_for_col"FROM dbo.authors
-- OKSELECT au_id+au_id AS 'alias_for_col'FROM dbo.authors
-- OKSELECT column_alias=expressionFROM dbo.authors
-- column_alias = expression syntax is used here, but ignored, because all rules at the violation line are suppressed.SELECT alias_for_col=au_id+au_id, au_id --IGNORE:*(LINE)FROM dbo.authors
-- Deprecated alias syntax is used here. Reported by SA0008SELECT 'alias_for_col'=au_id+au_idFROM dbo.authors
DECLARE @variable intSELECT @variable = 1Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0235 : Consider using the AS keyword to specify a column alias instead of the column_alias = expression syntax. | 18 | 23 |