Transact-SQL formatting standard (encoding style)-about Aliases
This article is a translation. For the original article, see:
Http://www.simple-talk.com/ SQL /t-sql-programming/transact-sql-formatting-standards (coding-styles)
SQL code formatting tool: http://www.sqlzoo.cn/sqlpp
Aliases (alias)
The role of an alias is to give a temporary name to the database object (or assign a temporary name to the dynamic computing column) for easy writing and reading.
Several factors should be taken into account when deciding how to use aliases:
- Are different policies used for different types of aliases? For example, whether the table alias is the same as the column alias. Are dynamic computing columns and general columns treated differently?
- What type of Alias Name is allowed? Can a single letter be used as an alias for a table? Can abbreviations be used as aliases of any type? Does the column alias use the same naming rules for aliases of other database objects?
- Is the AS mandatory or unavailable?
In the following example, the column alias (FullName) is a compound word, but the table alias is a single letter:
SELECT
(
C.
FirstName +
''
+
C.
LastName)
AS
FullName,
E.
LoginID,
E.
Title
FROM
HumanResources.
Employee
E
INNER
JOIN
Person.
Contact
C
ON
E.
ContactID =
C.
ContactID
ORDER
BY
C.
LastName
In complex joins, the alias of a single letter sometimes makes the code difficult to read, because it does not always intuitively represent the Association between tables and columns. The following example uses more meaningful abbreviations to name the table alias:
SELECT
(
Cnt.
FirstName +
''
+
Cnt.
LastName)
FullName,
Emp.
LoginID,
Emp.
Title
FROM
HumanResources.
Employee emp
INNER
JOIN
Person.
Contact cnt
ON
Emp.
ContactID =
Cnt.
ContactID
ORDER
BY
Cnt.
LastName
Note the use of the AS keyword in the two examples. The first example uses the AS keyword. The second one does not exist. Again, your formatting standard should specify whether to use the AS keyword.