SA0008 : Deprecated syntax string_alias = expression

Column aliasing syntax that assigns an alias name as a string value (e.g., ‘alias name’ = expression) is deprecated in SQL Server. It is recommended to use alternative aliasing syntax for forward compatibility.

Description

The syntax for column aliasing ‘alias name’ = expression is deprecated in SQL Server and may be discontinued in future version of SQL Server. It should be avoided for forward compatibility and adherence to best practices.

For example:

SQL
1-- Example of deprecated aliasing
2SELECT 'alias_for_col' = au_id + au_id
3FROM dbo.authors;

The syntax used in this query is problematic as it employs a string value for aliasing, which is not considered best practice and may be phased out in future versions of SQL Server. Proper aliasing should use one of the aliasing syntax alternatives.

  • Using deprecated syntax can reduce code readability and cause maintenance challenges.

  • Future versions of SQL Server may remove support for this syntax, leading to compatibility issues.

How to fix

To resolve issues identified by the rule, update the deprecated column aliasing syntax to alternative and supported syntaxes.

Follow these steps to address the issue:

  1. Identify the deprecated syntax in your queries where string values are used for aliasing, such as ‘alias_for_col’ = expression.

  2. Replace the deprecated aliasing syntax with the standard AS keyword or quoted identifiers. Use AS to clearly define the alias.

  3. Test the updated query to ensure that the aliasing change does not affect the query results or performance.

Recommended alternatives are:

SQL
1expression [AS] column_alias
2expression [AS] [column_alias]
3expression [AS] "column_alias"
4expression [AS] 'column_alias'
5column_alias = expression
6[column_alias] = expression

For example:

SQL
1-- Example of corrected query
2SELECT au_id + au_id AS [alias_for_col]
3FROM dbo.authors;

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, Deprecated Features, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1-- Deprecated alias syntax is used here
 2SELECT     'alias_for_col'=au_id+au_id
 3FROM       dbo.authors
 4
 5-- Deprecated alias syntax is used here,but ignored, because all rules at the violation line are suppressed
 6SELECT     'alias_for_col'=au_id+au_id, au_id --IGNORE:*(LINE)
 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     au_id+au_id AS "alias_for_col"
19FROM       dbo.authors
20
21-- OK
22SELECT     au_id+au_id AS 'alias_for_col'
23FROM       dbo.authors
24
25-- OK
26SELECT     column_alias=expression
27FROM       dbo.authors

Analysis Results
  Message Line Column
1 SA0008 : Deprecated syntax string_alias = expression. 2 11
See Also

Other Resources