Oracle sql: Drop table column shows error: invalid identifier?

Oracle sql: Drop table column shows error: invalid identifier?

当我试图在一个语句中删除多个列时出现此错误:-

    ALTER TABLE "RLX_ROLES" DROP COLUMN ("test","test_2");

但是如果我把它们一个一个地放下,它就会起作用:-

    ALTER TABLE "RLX_ROLES" DROP COLUMN "test";
    ALTER TABLE "RLX_ROLES" DROP COLUMN "test_2";

为什么会这样?我的语法不正确吗?

编辑:- 我也试过:-

    ALTER TABLE RLX_ROLES DROP (test,test_2);

    ALTER TABLE RLX_ROLES DROP COLUMN (test,test_2);

更新:-

显然,我拼错了其中一个列名称,对此深表歉意。

您必须删除双引号。它们不是必需的,如果您想删除多个列,则必须删除关键字 column

ALTER TABLE RLX_ROLES DROP (test,test_2);

有关 alter 语法的详细信息,请参阅 here

您必须使用以下语句来删除多个列:

alter table table_name drop (col_name1, col_name2); 

那么你必须删除语句中的 "column" 关键字。

Drop table column shows error: invalid identifier?

ALTER TABLE "RLX_ROLES" DROP COLUMN ("test","test_2");

您使用的 alter 语句中有两处不正确。

  1. COLUMN 关键字不需要删除多列。
  2. 不需要双引号

使用双引号:

SQL> ALTER TABLE t DROP ("b", "c");
ALTER TABLE t DROP ("b", "c")
                         *
ERROR at line 1:
ORA-00904: "c": invalid identifier


SQL>

因此,删除列关键字和双引号。

有关 Removing Columns from Tables 的所有记录。

Removing Columns from Tables

When you issue an ALTER TABLE...DROP COLUMN statement, the column descriptor and the data associated with the target column are removed from each row in the table. You can drop multiple columns with one statement.

删除单列:

SQL> CREATE TABLE t(
  2  A NUMBER,
  3  b NUMBER,
  4  c NUMBER
  5  );

Table created.

SQL>
SQL> ALTER TABLE t DROP column c;

Table altered.

删除多列:

SQL> drop table t purge;

Table dropped.

SQL> CREATE TABLE t(
  2  a NUMBER,
  3  b NUMBER,
  4  c NUMBER
  5  );

Table created.

SQL>
SQL> ALTER TABLE t DROP (b, c);

Table altered.

SQL>

可能是您无意中创建了带有区分大小写的列名称的 table(通过对它们使用双引号)。如果是这种情况,您必须在 DROP 语句中使用双引号和准确拼写:

create table RLX_ROLES("test" number, "Test_2" number, test3 number);

alter table RLX_ROLES drop ("Test_2","test");

要生成删除所有拼写正确的列的 SQL 语句,您可以使用此语句(请注意 运行 此语句未更改会引发错误,因为您不能删除所有列table 中的列):

select 'alter table ' || table_name ||
  ' drop (' || 
  listagg('"' || utc.column_name || '"', ',') within group (order by utc.column_name) ||
  ');'
from user_tab_cols utc
where table_name = 'RLX_ROLES'
group by table_name;

这会查询 user_tab_cols 字典视图以获取 RLX_ROLES table 的所有列名称,并使用 LISTAGG 聚合函数连接它们。