Transact-SQL formatting standard (encoding style)-about Aliases

Source: Internet
Author: User
Tags aliases

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.