SA0119 : Consider aliasing all table sources in the query
Introduction
Section titled “Introduction”Avoid using unaliased table sources in SQL statements for better clarity and maintenance.
Description
Section titled “Description”It’s common practice to alias table sources in the FROM clause of SELECT , UPDATE , and DELETE statements. Not using aliases can lead to confusion and make the SQL code harder to read and maintain, especially in complex queries with multiple tables.
For example:
-- Example of a query missing table aliasesSELECT FirstName, LastName FROM EmployeesJOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;This query does not use aliases, which can cause confusion, especially when multiple tables have similar column names or when queries grow in complexity. Best practices suggest using aliases to improve query readability and manageability.
-
Lack of aliases can make it difficult to track which columns belong to which table, increasing the chance of errors.
-
Using aliases can improve performance by making query plans more efficient and easier to optimize.
How to fix
Section titled “How to fix”Improve the readability and maintainability of SQL code by aliasing all table sources in your queries.
Follow these steps to address the issue:
1.Identify all tables used in your FROM clause within SELECT , UPDATE , and DELETE statements.
2.Assign an alias to each table. An alias is a shorthand reference that simplifies reading and writing queries. Use a meaningful alias for each table to make your SQL statements more intuitive.
3.Replace all explicit table names in your query with their corresponding aliases. Ensure consistency across the query to maintain clarity.
For example:
-- Example of a corrected query using table aliasesSELECT E.FirstName, E.LastNameFROM Employees AS EJOIN Departments AS D ON E.DepartmentID = D.DepartmentID;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| IgnoreSingleTableSources | The queries accessing single tables will be ignored. | yes |
| IgnoreTableValuesFunctions | The not aliased table valued functions will be ignored. | yes |
| IgnoreSystemObjects | The not aliased system tables or views will be ignored. | yes |
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”5 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 Sales.CustomerINNER JOIN Sales.vStoreWithAddresses AS sa ON CustomerID = sa.BusinessEntityIDWHERE TerritoryID = 5
SELECT *FROM Sales.Customer /*IGNORE:SA0119*/INNER JOIN Sales.vStoreWithAddresses AS sa ON CustomerID = sa.BusinessEntityIDWHERE TerritoryID = 5
SELECT *FROM HumanResources.EmployeeUNIONSELECT *FROM HumanResources.EmployeeOPTION (MERGE UNION);
SELECT * FROM Sales.SalesOrderHeader WITH (NOLOCK)Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0119 : Table source does not have an alias. Consider aliasing all table sources in the query. | 2 | 11 |