项目中遇到这样的需求:需要清除oracle数据库中当前登录用户下的所有具有某一前缀的表。
于是写了个存储过程。
案例描述:
删除当前用户下所有具有“SC_”前缀的表
案例分析:
获取到所有具有该前缀的表名,然后依次drop这些表
解决方案
存储过程的代码如下:
create or replace procedure droptab_beginwith_SC_ is
--author:zhiyelee
--Blog:http://tsnrose.com
--useage: delete from current database tables starting with 'SC_' of current user
begin
dbms_output.put_line('Begin');
dbms_output.put_line('//===============================================');
for tab in(select table_name from tabs where table_name like 'SC\_%' escape '\') loop
begin
execute immediate 'drop table '||tab.table_name;
dbms_output.put_line('successfully drop table '||tab.table_name);
end;
end loop;
dbms_output.put_line('//===============================================');
dbms_output.put_line('Done');
commit;
end droptab_beginwith_SC_;
背景知识
1、Oracle中获取当前用户所有表的语句
select table_name from tabs ;
2、oracle中转义符的用法:
escape关键字经常用于使某些特殊字符,如通配符:’%',’_'转义为它们原来的字符的意义,被定义的转义字符通常使用’\',但是也可以使用其他的符号。
select table_name from tabs where table_name like 'SC\_%' escape '\';
这儿使用’\'作转义符,当然这儿也可以换成’/'等其它字符。
//===============================================