Usage of sqlserver as

Time:2020-9-26

SQL statements can specify aliases for table or column names. (aliases are sometimes referred to as export columns. Whether they are export columns or aliases, they represent the same thing.)

SQL alias
SQL aliases are used to provide temporary names for tables or columns in tables.  
SQL aliases are often used to make table or column names more readable.  
SQL an alias exists only during the query.  
Aliases are given using the as keyword.  

Aliases are recommended when
The query involves multiple tables

For query functions

You need to put two or more columns together

Long column name or poor readability

grammar

1. Alias syntax for table names:

Select column name
From table name as alias;

2. Alias syntax for column names

Select column name as alias
From table name;

3. Aliasing syntax for table and column names

Select column name as alias
From table name as alias;

Examples of grammar

1. Use table name alias

There are two tables: people and product_ Orders”。 Give them aliases “P” and “Po” respectively. List all orders for “John Adams”.


SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p, Product_Orders AS po
WHERE p.LastName='Adams' AND p.FirstName='John';

2. Use column name alias

Query the LastName column (which defines the alias’ last name ‘) and the firstname column (defines the alias’ first name’) in the persons table and outputs all the result values.

Select LastName as last name, firstname as first name
FROM Persons

3. Use both table and column names

Define the alias’ a ‘for the city table, and use the table alias to query all information about the ID column in the table (define alias B for the ID column).


SELECT A.ID AS B
FROM city AS A;

Here’s what others have added

1 Overview

This article briefly analyzes the basic usage of as in SQL server.

2. Specific analysis

2.1 defining variable types

DECLARE @UserName AS VARCHAR(50)=’Alan_beijing’

2.2 alias

2.2.1 alias the result set column

code:

SELECT OrderID,CustID,OrderDate
FROM Orders

result:

code:

Select OrderID as’ order ID ‘, custid as’ customer ID’, OrderDate as’ order date ‘
FROM Orders

result:

2.2.2.2 alias table

SELECT O.OrderID,O.CustID,O.OrderDate
FROM Orders AS O

2.2.3 alias subquery

SELECT ChildQuery_Orders.OrderDate
FROM
(SELECT *
FROM Orders
WHERE CustID>=5) AS ChildQuery_Orders 

2.3 conversion type

2.3.1 cast type conversion

SELECT CAST(‘110’ AS INT)  –110

2.3.2 convert type conversion

SELECT CONVERT(decimal, ‘123.6’) — 123

2.4 create a view to represent the source of the base table

CREATE VIEW ViewDemo
AS
SELECT * FROM Orders

2.5 others (to be supplemented later)

3. References
[01] inside story of Microsoft sqlserver 2008: T-SQL language foundation
[02] technical insider of Microsoft sqlserver 2008: T-SQL query
4. Copyright

Supplement by Alan_ Beijing.