简体   繁体   English

在 select 子句中使用 equals 设置列别名

[英]Using equals in a select clause to set column aliases

Today I saw someone using this syntax (A) to set column aliases in a sql select.今天看到有人用这个语法(A)在sql select中设置列别名。

A一种

Select [ColumnAlias] = Column1 FROM Table

B

Normally I use通常我使用

Select Column1 AS [ColumnAlias] FROM Table

I've used = to set local variables, but never as aliases.我使用=来设置局部变量,但从来没有作为别名。 Select @LocalVar = Column1 FROM Table

Is there any reason to prefer B over A?有什么理由比 A 更喜欢 B 吗? I can't seem to find much on A searching.我似乎无法在 A 搜索中找到太多。

Not many RDBMS support the equals syntax, but it is commonly used by VB/C# application developers who then learn MS SQL Server due to the similarity to assigning a value to a variable, that is certainly one way to look at it, and although I cannot find a reference, this syntax was probably adopted to encourage developers to adopt the MS SQL platform.支持equals语法的 RDBMS 并不多,但由于与为变量赋值的相似性,后来学习 MS SQL Server 的 VB/C# 应用程序开发人员通常使用它,这当然是一种看待它的方式,虽然我找不到参考,这种语法可能是为了鼓励开发人员采用 MS SQL 平台而采用的。

The problem is that this usage is ambiguous because the same syntax can be used to actually assign the value of a column into a variable, or in an UPDATE statement to assign the value into another column:问题在于这种用法是不明确的,因为可以使用相同的语法将列的值实际分配给变量,或者在UPDATE语句中将值分配给另一列:

-- assign the variable the value from Column1
SELECT @MyVar = Column1 FROM Table

-- Update Column2 with the value from Column1
UPDATE Column2 = Column1 FROM Table

Using the AS keyword is supported in all SQL RDBMS, it is a lot less ambiguous as similar syntax only exists inside CAST functions.所有 SQL RDBMS 都支持使用AS关键字,因为类似的语法只存在于CAST函数中,所以歧义要少得多。

AS is more expressive, it is very clear what the intent is and cannot be mistaken to be an assignment. AS比较有表现力,很清楚意图是什么,不能误认为是赋值。

Using AS is also considered more standard because the same syntax can be used to alias tables, views and other record sets in the SQL query.使用AS也被认为是更标准的,因为相同的语法可以用于别名表、视图和 SQL 查询中的其他记录集。

It is worth noting that AS is technically optional in most RDBMS, so you could leave it out altogether, but doing this can lead to other issues, especially if the alias you wanted to use actually exists as a real name of a column or a table.值得注意的是, AS在大多数 RDBMS 中在技术上是可选的,因此您可以将其完全省略,但这样做可能会导致其他问题,尤其是当您要使用的别名实际上作为列或表的真实名称存在时. Reasons to use 'AS' are documented here: https://www.databasestar.com/sql-alias/此处记录了使用“AS”的原因: https://www.databasestar.com/sql-alias/

So while these variants will all work to alias a column in SQL Server:因此,虽然这些变体都可以为 SQL 服务器中的列添加别名:

SELECT Column1 AS [alias] from Table
SELECT Column1 AS 'alias' from Table
SELECT Column1 [alias] FROM Table
SELECT [alias] = Column1 FROM Table

Only AS (or omitting it) works for aliasing a table只有AS (或省略它)才能为表格起别名

SELECT Column1 AS [alias] from Table as t
SELECT Column1 [alias] FROM Table t
SELECT Column1 alias FROM Table [t]

The following will NOT work for table aliases以下不适用于表别名
Note here that we can't alias a table to a string value, it must be a tokenized name这里要注意,我们不能将表别名为字符串值,它必须是标记化的名称

SELECT Column1 AS 'alias' from Table AS 't'
SELECT [alias] = Column1 FROM t = Table

Using AS is the most commonly supported syntax across all tables for any type of alias in a query, not just columns.对于查询中任何类型的别名,使用AS是所有表中最普遍支持的语法,而不仅仅是列。 For this reason you will find it as the most commonly documented syntax as well.因此,您会发现它也是最常记录的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM