如何删除用户下的所有没有数据的空表 

解决方案 »

  1.   

    drop table tb;
    不可以么?还是要增加一条数据
      

  2.   

    怎么样可以用一个SQL语句删除这个用户下所有没有数据的空表?
      

  3.   

    写个过程啊,很简单的
    先判断表是否有记录,然后DROP,有什么难度的吗
      

  4.   

    写存储过程罢,从user_tables表中查询num_rows(表中记录个数)列的值为0的table_name(表名),然后使用动态DDL语句删除表。
      

  5.   

    建议不要使用user_tables的方式来判断,虽然快,但是在第一次用脚本转移数据库结构和数据过来的话,有可能表的信息为空。建议使用
    if exists (select 1 from table where rownum < 2);
      

  6.   

    declare
      tableName varchar2(100) := '';
    begin
      for tableList in (select table_name from user_tables where num_rows = 0) loop
        tableName := 'drop table ' || tableList.Table_Name || ';';
        dbms_output.put_line(tableName);
        --具体的执行语句,请自己执行,我可不敢在我的数据库里试
        --execute immediate tableName;
      end loop;
    end;
      

  7.   


    declare 
      v_cnt number(18,0);
      emp_table varchar2(23767);
      cursor my_cur
      is 
      select table_name  from user_tables;
       e my_cur%rowtype;
      begin
       if not my_cur%ISOPEN then 
          open my_cur;
       end if;
       fetch my_cur into e;
       while my_cur%FOUND loop
        execute immediate 'select count(1) from '|| e.table_name ||' where 1=1'  into v_cnt;
      if v_cnt=0 then 
        begin
       -- dbms_output.put_line('表: '||e.table_name ||' 是空表');
      emp_table:=emp_table||', '||e.table_name;
      end;
       -- else 
         -- dbms_output.put_line('表: '||e.table_name ||' 不是空表,行数为 '||to_char(v_cnt)||' ');
          end if;
         fetch my_cur into e;
      end loop;
        dbms_output.put_line('drop table '||emp_table);
      close my_cur;
    end;drop table 后面 多了个逗号 去掉了就可以执行 也可以自行加上 purge选项