如何在 Oracle 中转义保留字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1162381/
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-10 02:02:07  来源:igfitidea点击:

How do I escape a reserved word in Oracle?

oraclereserved-words

提问by Spence

In TSQL I could use something like Select [table] from tablenameto select a column named "table".

在 TSQL 中,我可以使用类似Select [table] from tablename选择名为“table”的列。

How do I do this for reserved words in oracle?

我如何为 oracle 中的保留字执行此操作?

Edit: I've tried square braces, double quotes, single quotes, and backquotes, they don't work...

编辑:我试过方括号、双引号、单引号和反引号,它们不起作用......

As a further clarification, I have a column which someone named comment. As this is a reserved word oracle is chucking a wobbly trying to select with it, its failing when parsing the query. I've tried Select "comment" from tablename but it didn't work. I'll check case and come back.

作为进一步澄清,我有一个专栏,有人将其命名为评论。由于这是一个保留字,oracle 正在尝试使用它进行选择,因此在解析查询时失败。我试过从表名中选择“评论”,但没有用。我会检查情况然后回来。

回答by eyelidlessness

From a quick search, Oracle appears to use double quotes (", eg "table") and apparently requires the correct case—whereas, for anyone interested, MySQL defaults to using backticks (`) except when set to use double quotes for compatibility.

通过快速搜索,Oracle 似乎使用双引号 ( ", eg "table")并且显然需要正确的大小写——然而,对于任何感兴趣的人,MySQL 默认使用反引号 (`),除非为了兼容性而设置为使用双引号。

回答by Jeffrey Kemp

Oracle normally requires double-quotes to delimit the name of identifiers in SQL statements, e.g.

Oracle 通常需要双引号来分隔 SQL 语句中的标识符名称,例如

SELECT "MyColumn" AS "MyColAlias"
FROM "MyTable" "Alias"
WHERE "ThisCol" = 'That Value';

However, it graciously allows omitting the double-quotes, in which case it quietly converts the identifier to uppercase:

但是,它允许省略双引号,在这种情况下,它会悄悄地将标识符转换为大写:

SELECT MyColumn AS MyColAlias
FROM MyTable Alias
WHERE ThisCol = 'That Value';

gets internally converted to something like:

在内部转换为类似的东西:

SELECT "ALIAS" . "MYCOLUMN" AS "MYCOLALIAS"
FROM "THEUSER" . "MYTABLE" "ALIAS"
WHERE "ALIAS" . "THISCOL" = 'That Value';

回答by Suresh G

double quotes worked in oracle when I had the keyword as one of the column name.

当我将关键字作为列名之一时,双引号在 oracle 中起作用。

eg:

例如:

select t."size" from table t 

回答by Andrew not the Saint

Oracle does use double-quotes, but you most likely need to place the object name in upper case, e.g. "TABLE". By default, if you create an object without double quotes, e.g.

Oracle 确实使用双引号,但您很可能需要将对象名称大写,例如“TABLE”。默认情况下,如果您创建一个没有双引号的对象,例如

CREATE TABLE table AS ...

Oracle would create the object as upper case. However, the referencing is not case sensitive unless you use double-quotes!

Oracle 会将对象创建为大写。但是,除非您使用双引号,否则引用不区分大小写!

回答by oualid

you have to rename the column to an other name because TABLEis reserved by Oracle.

您必须将该列重命名为其他名称,因为它TABLE是 Oracle 保留的。

You can see all reserved words of Oracle in the oracle view V$RESERVED_WORDS.

在oracle 视图中可以看到Oracle 的所有保留字V$RESERVED_WORDS