您的位置:首页 > 数据库

使用游标删除数据库表

2014-03-11 15:40 253 查看
create or replace procedure PROC_DROPTABLE_BIEE

as

--引用user_tables表中的tableName的类型;

--集合处理方式

type record_type is table of user_tables%rowtype;

record_table record_type;

--表名

tableName user_tables.table_name%type;

mycount number(10);

cursor biee_tables_cur is select t.* from user_tables t where t.TABLE_NAME like '%T_BI_%';

rowdata biee_tables_cur%rowtype;

begin

select count(*) into mycount from user_tables t where t.TABLE_NAME like '%T_BI_%';

--存在数据

if (mycount > 0) then

dbms_output.put_line('共有 ' || mycount || ' 条表名包含 T_BI 的表记录');

open biee_tables_cur;

fetch biee_tables_cur bulk collect into record_table;

close biee_tables_cur;

--for Loop

for record_table in biee_tables_cur

Loop

dbms_output.put_line('for loop:' || record_table.table_name );

end Loop;

--while Loop

open biee_tables_cur;

FETCH biee_tables_cur into rowdata;

while biee_tables_cur%found

Loop

dbms_output.put_line('while loop:' || rowdata.table_name );

fetch biee_tables_cur into rowdata;

end Loop;

close biee_tables_cur;

for i in 1.. record_table.count loop

--获取当前表名

tableName:= record_table(i).TABLE_NAME;

--检查是否存在该表

select count(*) into mycount from user_tables t where t.TABLE_NAME=tableName;

--存在则删除

if (mycount > 0) then

dbms_output.put_line('删除表名为 "' || tableName || '" 的表');

execute immediate 'drop table ' || tableName;

else

dbms_output.put_line('不存在表名为 "' || tableName || '" 的表');

end if;

end loop;

--不存在对应数据

else

dbms_output.put_line('不存在表名包含 T_BI 的表记录');

end if;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: