使用游标删除数据库表

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;
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值