Rename column named TYPE, LEVEL in sqlplus

我们两清 提交于 2019-12-25 04:51:13

问题


This needs to be done in sql plus.

Hi, I'm struggling to rename two columns in my table. They are named "TYPE" and "LEVEL".

This needs to be done in sql plus with no exception.

The following does not work (works in sql developer tho):

alter table client rename column level to clevel;
alter table client rename column "level" to clevel;

回答1:


LEVEL is an Oracle keyword, though not reserved. If you want to use it as an object name then you need to represent the name of an object with a quoted identifier using double quotation marks whenever you refer to that object.

SQL> SELECT keyword, reserved FROM V$RESERVED_WORDS WHERE keyword='LEVEL';

KEYWORD                        R
------------------------------ -
LEVEL                          N

SQL>

That is the reason if you use the keyword LEVEL as nonquoted identifier, it will throw an error:

SQL> CREATE TABLE t(level NUMBER);
CREATE TABLE t(level NUMBER)
               *
ERROR at line 1:
ORA-00904: : invalid identifier

Now, per the documentation about Database Object Names and Qualifiers, if you create the object using double-quotation marks, it becomes case sensitive and must be always used the same way wherever the object is referenced.

For example,

SQL> CREATE TABLE t1("level" NUMBER);

Table created.

SQL>
SQL> ALTER TABLE t1 RENAME COLUMN "level" to clevel;

Table altered.

SQL>
SQL> CREATE TABLE t2("LEVEL" NUMBER);

Table created.

SQL>
SQL> ALTER TABLE t2 RENAME COLUMN "LEVEL" to clevel;

Table altered.

SQL>

It is better not to use the keyword, and give a proper naming convention.

SQL> CREATE TABLE t(clevel NUMBER);

Table created.

SQL>



回答2:


You can also execute sql scripts in sqlplus. Just save your statement in a file and then use the following command:

@{file-path}

See also this link



来源:https://stackoverflow.com/questions/30457391/rename-column-named-type-level-in-sqlplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!