可以用一个带参数的存储过程和查询表格是否存在的语句来实现在drop之前检查表格是否存在 。下面这条语句用于查询表格是否存在在某一个用户下面。如果检查的表格存在返回1,不存在返回0.
select count(*) from from ALL_TABLES where OWNER=upper('owner_name') and TABLE_NAME=upper('table_name')
下面是存储过程,如果num的值是1就执行drop语句。
create or replace procedure table_exit(owner in varchar,tablename in varchar, num out number) is
v_sql varchar2(2000);
begin
v_sql := 'SELECT COUNT(*) FROM ALL_TABLES WHERE OWNER ='||chr(39)||owner||chr(39)||'AND TABLE_NAME ='||''''||tablename||'''';
execute immediate v_sql into num;
if num = 1 then
execute immediate 'drop table '||owner||'.'||tablename;
dbms_output.put_line('drop table '||owner||'.'||tablename);
end if;
end table_exit;
在创建表格之前调用该存储函数即可:
declare
tablename varchar(20);
owner varchar(20);
n number;
begin
t:= upper('table');
o:= upper('temp');
table_exit(owner, tablename,n);
end;