小弟一直用sqldeveloper开发,发现想删全部表的时候特别麻烦,于是想到用pl/sql写了下面的代码:
- SQL code
declarecursor tab_cur is select 'drop table '||table_name||';' from user_tables;tab_row varchar2(100);beginopen tab_cur;while tab_cur%foundloopfetch tab_cur into tab_row;execute immediate tab_row;end loop;close tab_cur;end;
可是执行却删不了任何表。
求高手指教这是为什么。
------解决方案--------------------
两点
1.'drop table '||table_name||';'分号不要
2.while tab_cur%FOUND这里的判断有问题
修改后代码如下:
- SQL code
declarecursor tab_cur is select 'drop table '||table_name from user_tables WHERE table_name='XIAODEJUN';tab_row varchar2(100);BEGINopen tab_cur;--while tab_cur%FOUNDLOOPfetch tab_cur into tab_row;EXIT WHEN tab_cur%NOTFOUND; execute immediate tab_row;end loop;close tab_cur;end;
------解决方案--------------------
declare
cursor tab_cur is select table_name from user_tables WHERE table_name='XIAODEJUN';
tab_row varchar2(100);
BEGIN
open tab_cur;
--while tab_cur%FOUND
LOOP
fetch tab_cur into tab_row;
EXIT WHEN tab_cur%NOTFOUND;
execute immediate 'drop table ' || tab_row;
end loop;
close tab_cur;
end;