如何处理看起来像 SQL 关键字的 SQL 列名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/285775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:14:17  来源:igfitidea点击:

How to deal with SQL column names that look like SQL keywords?

sqlsql-server

提问by Nathan H

One of my columns is called from. I can't change the name because I didn't make it. Am I allowed to do something like SELECT from FROM TableNameor is there a special syntax to avoid the SQL Server being confused?

我的一列名为from. 我不能更改名称,因为我没有做到。我可以做类似的事情SELECT from FROM TableName还是有特殊的语法来避免 SQL Server 被混淆?

回答by tvanfosson

Wrap the column name in brackets like so, frombecomes [from].

像这样将列名括在方括号中,from变成 [from]。

select [from] from table;

It is also possible to use the following (useful when querying multiple tables):

还可以使用以下内容(在查询多个表时很有用):

select table.[from] from table;

回答by some

If it had been in PostgreSQL, use double quotes around the name, like:

如果它已经在 PostgreSQL 中,请在名称周围使用双引号,例如:

select "from" from "table";

Note: Internally PostgreSQL automatically converts all unquoted commands and parameters to lower case. That have the effect that commands and identifiers aren't case sensitive. sEleCt * from tAblE;is interpreted as select * from table;. However, parameters inside double quotes are used as is, and therefore ARE case sensitive: select * from "table";and select * from "Table";gets the result from two different tables.

注意:PostgreSQL 在内部自动将所有未加引号的命令和参数转换为小写。这会导致命令和标识符不区分大小写。从表中选择 *;被解释为select * from table; . 但是,双引号内的参数按原样使用,因此区分大小写:select * from "table"; 从“表”中选择*;从两个不同的表中获取结果。

回答by Cade Roux

While you are doing it - alias it as something else (or better yet, use a view or an SP and deprecate the old direct access method).

在您执行此操作时 - 将其别名为其他内容(或者更好的是,使用视图或 SP 并弃用旧的直接访问方法)。

SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName

回答by Sunil Kapil

These are the two ways to do it:

这是两种方法:

  1. Use back quote as here:
  1. 在这里使用反引号:

SELECT `from` FROM TableName

SELECT `from` FROM 表名

  1. You can mention with table name as:
  1. 您可以使用表名提及:

SELECT TableName.from FROM TableName

SELECT TableName.from FROM TableName

回答by Eigir

Your question seems to be well answered here, but I just want to add one more comment to this subject.

您的问题在这里似乎得到了很好的回答,但我只想对这个主题再添加一条评论。

Those designing the database should be well aware of the reserved keywords and avoid using them. If you discover someone using it, inform them about it (in a polite way). The keyword here is reservedword.

那些设计数据库的人应该很清楚保留关键字并避免使用它们。如果您发现有人使用它,请告知他们(以礼貌的方式)。这里的关键字是保留字。

More information:

更多信息:

"Reserved keywords should not be used as object names. Databases upgraded from earlier versions of SQL Server may contain identifiers that include words not reserved in the earlier version, but that are reserved words for the current version of SQL Server. You can refer to the object by using delimited identifiers until the name can be changed." http://msdn.microsoft.com/en-us/library/ms176027.aspx

"保留关键字不应用作对象名称。从早期版本的 SQL Server 升级的数据库可能包含标识符,其中包含早期版本中未保留的词,但这些词是 SQL Server 当前版本的保留词。您可以参考对象,直到名称可以更改为止。” http://msdn.microsoft.com/en-us/library/ms176027.aspx

and

"If your database does contain names that match reserved keywords, you must use delimited identifiers when you refer to those objects. For more information, see Identifiers (DMX)." http://msdn.microsoft.com/en-us/library/ms132178.aspx

“如果您的数据库确实包含与保留关键字匹配的名称,则在引用这些对象时必须使用分隔标识符。有关详细信息,请参阅标识符 (DMX)。” http://msdn.microsoft.com/en-us/library/ms132178.aspx

回答by John Baughman

If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.

如果您使用的是 SQL Server,则只需将方括号括在列名或表名周围即可。

select [select]
from [table]

回答by Kun Wu

In Apache Drill, use backquotes:

在 Apache Drill 中,使用反引号:

select `from` from table;

回答by Muneeb Hassan

I have also faced this issue. And the solution for this is to put [Column_Name] like this in the query.

我也遇到过这个问题。对此的解决方案是将 [Column_Name] 像这样放在查询中。

string query= "Select [Name],[Email] from Person";

So it will work perfectly well.

所以它会工作得很好。

回答by preyingrazor

Hi I work on Teradata systems that is completely ANSI compliant. Use double quotes " " to name such columns.

嗨,我在完全符合 ANSI 的 Teradata 系统上工作。使用双引号 " " 来命名这些列。

E.g. typeis a SQL reserved keyword, and when used within quotes, typeis treated as a user specified name.

egtype是一个 SQL 保留关键字,当在引号中使用时,type被视为用户指定的名称。

See below code example:

请参阅下面的代码示例:

CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)

--type is a SQL reserved keyword

TYPE

--see? now to retrieve the column you would use:

SEL "type" FROM alpha1

回答by Fabricio PH

I ran in the same issue when trying to updatea column which name was a keyword. The solution above didn't help me. I solved it out by simply specifying the name of the table like this:

我在尝试更新名称为关键字的列时遇到了同样的问题。上面的解决方案对我没有帮助。我通过简单地指定表的名称来解决它:

UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')