Oracle 的drop table if exists功能

Oracle创建表时,常遇到先删除后创建的情况,而它又没有drop table... if exists语法。为此可以使用user_objects数据字典和动态sql语句实现类似的功能,如下所示:

create or replace procedure proc_dropifexists(
    p_table in varchar2
) is
    v_count number(10);
begin
   select count(*)
   into v_count
   from user_objects
   where object_name = upper(p_table);

   if v_count > 0 then
      execute immediate 'drop table ' || p_table ||' purge';
   end if;
end;
/

--调用
exec proc_dropifexists('mytable');

你可能感兴趣的:(数据库技术)