Oracle-在一个查询中删除多个表
我在一个数据库中有50个表,因为我只需要6个表Oracle-在一个查询中删除多个表,oracle,ddl,Oracle,Ddl,我在一个数据库中有50个表,因为我只需要6个表 如何通过一个查询删除剩余的表?使用类似的方法,因为oracle中没有直接的命令或方法来执行此操作 begin for rec in (select table_name from all_tables where table_name like '%ABC_%' ) loop execute immediate 'drop table '||
如何通过一个查询删除剩余的表?使用类似的方法,因为oracle中没有直接的命令或方法来执行此操作
begin
for rec in (select table_name
from all_tables
where table_name like '%ABC_%'
)
loop
execute immediate 'drop table '||rec.table_name;
end loop;
end;
/
您可以使用以下查询生成下拉表命令列表:
SELECT 'DROP TABLE ' || table_name || ';' FROM user_tables;
然后删除要保留的六个表并执行其他命令。或者向查询中添加WHERE table\u name NOT IN(…)
子句
希望有帮助。首先使用要保留的表名运行此查询
SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )
AS statement FROM information_schema.tables
WHERE table_schema = 'mydatabase' AND table_name not in ('table1', 'table2', 'table3');
此查询将为您提供DROP表查询
,
\ndrop表格
。请注意,单词表后面有一个空格。全部替换放下表格
,然后单击代码>在上一个之后
SELECT 'DROP TABLE ' || table_name || ' PURGE ;' DB_DEATH FROM user_tables;
如果要保留的表是
keep_tab1
、keep_tab2
,等等,或者以ASB开头像一样使用
regexp\u。这里仅显示一个类似于
即使在删除其中一个表时发生错误,循环仍将继续。您可以 必须多次运行此脚本,因为在没有先删除引用表的情况下删除主表时可能会发生错误
begin
for rec in (
select table_name from user_tables
where not
regexp_like(table_name, 'keep_tab1|keep_tab2|^ASB')
)
loop
begin
execute immediate 'drop table '||rec.table_name;
exception when others then
dbms_output.put_line(rec.table_name||':'||sqlerrm);
end;
end loop;
end;
如果我是你,我将导出这六个表,删除所有内容,然后还原这六个表。你没有像SQL Developer或Toad这样的工具吗?只需单击表(如果愿意,可以使用shift和ctrl),然后单击“删除”。@ThorstenKettner我有sql developer,但问题是我可以选择多个表,但在选择多个表后,它并没有提供“删除”选项。哦,太糟糕了。对不起,我不知道。我以为所有这些工具都会提供这种简单的功能。我正在使用Toad。最好的解决方案是@Raptor:简单、快速、安全……我还将添加级联约束,并使用
user\u tables
而不是all\u tables
。否则,可以在其他架构中删除具有相同名称的表(如果您有删除权限)。@Deepu-in-where子句中我们可以使用“not-in”运算符,然后我们将所有六个名称都放在那里?@我自己:好的,这是不可能的,因为在drop-table
命令中没有架构名称。在最坏的情况下,它将导致一个错误,因为表已经被删除,但这并不是那么悲惨:)@vim yes它应该是可能的,它在我运行时给出了错误。SQL错误:ORA-00942:表或视图不存在。我使用此查询选择“DROP TABLE”| | TABLE| U name | |”来自pmd_dba;这里pmd_dba是数据库否,您必须使用user_表
。它是您是所有者的所有表的视图。
begin
for rec in (
select table_name from user_tables
where not
regexp_like(table_name, 'keep_tab1|keep_tab2|^ASB')
)
loop
begin
execute immediate 'drop table '||rec.table_name;
exception when others then
dbms_output.put_line(rec.table_name||':'||sqlerrm);
end;
end loop;
end;