建议不要使用user_tables的方式来判断,虽然快,但是在第一次用脚本转移数据库结构和数据过来的话,有可能表的信息为空。建议使用 if exists (select 1 from table where rownum < 2);
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;
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选项
不可以么?还是要增加一条数据
先判断表是否有记录,然后DROP,有什么难度的吗
if exists (select 1 from table where rownum < 2);
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;
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选项