SA0235 : Consider using the AS keyword to specify a column alias instead of the column_alias = expression syntax

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

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:

SQL
1-- Example of problematic query using assignment operator for aliasing
2SELECT column1 = expression1
3FROM 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

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:

SQL
1-- Problematic query using assignment operator for aliasing
2SELECT column1 = expression1
3FROM TableName;
4
5-- Corrected query using AS keyword for aliasing
6SELECT expression1 AS column1
7FROM TableName;

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
 1-- OK
 2SELECT     au_id+au_id AS alias_for_col
 3FROM       dbo.authors
 4
 5-- OK
 6SELECT     au_id+au_id AS [alias_for_col]
 7FROM       dbo.authors
 8
 9-- OK
10SELECT     au_id+au_id AS "alias_for_col"
11FROM       dbo.authors
12
13-- OK
14SELECT     au_id+au_id AS 'alias_for_col'
15FROM       dbo.authors
16
17-- OK
18SELECT     column_alias=expression
19FROM       dbo.authors
20
21-- column_alias = expression syntax is used here, but ignored, because all rules at the violation line are suppressed.
22SELECT     alias_for_col=au_id+au_id, au_id --IGNORE:*(LINE)
23FROM       dbo.authors
24
25
26-- Deprecated alias syntax is used here. Reported by SA0008
27SELECT     'alias_for_col'=au_id+au_id
28FROM       dbo.authors
29
30DECLARE @variable int
31SELECT @variable = 1

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
See Also

Other Resources