Home  >  Article  >  Database  >  sql存储过程删除用户下所有对象语句

sql存储过程删除用户下所有对象语句

WBOY
WBOYOriginal
2016-06-07 17:48:411040browse

一个开发中可能会碰到的sql语句,我们利用sql存储过程删除用户下所有对象,有需要的同学可以参考一下哦,很简单的。

 代码如下 复制代码


create or replace procedure drop_all as  cursor cur_obj is
    uo.OBJECT_NAME, uo.OBJECT_TYPE
      from user_objects uo
     where uo.OBJECT_NAME not in ('DROP_ALL')
       and uo.OBJECT_TYPE not in ('LOB');

/*  cursor cur_tablespace is
    select ut.TABLESPACE_NAME
      from user_tablespaces ut
     where ut.TABLESPACE_NAME not in
           ('SYSTEM', 'SYSAUX', 'UNDOTBS1', 'TEMP', 'USERS');*/

  v_obj_name         user_objects.OBJECT_NAME%type;
  v_obj_type         user_objects.OBJECT_TYPE%type;
/*  v_tablespaces_name user_tablespaces.TABLESPACE_NAME%type;*/
  sql_str1           varchar2(2000);
/*  sql_str2           varchar2(2000);*/
begin
  open cur_obj;
  loop
    fetch cur_obj
      into v_obj_name, v_obj_type;
    exit when cur_obj%notfound;
    sql_str1 := 'drop ' || v_obj_type || ' ' || v_obj_name;
    execute immediate sql_str1;
  end loop;
  close cur_obj;
/*  open cur_tablespace;
  loop
    fetch cur_tablespace
      into v_tablespaces_name;
    exit when cur_tablespace%notfound;
    sql_str2 := 'drop tablespace ' || v_tablespaces_name ||
                ' including contents';
    execute immediate sql_str2;
  end loop;
  close cur_tablespace;*/
end drop_all;

  这个存储过程可以一把删掉用户下几乎所有的对象。注释里的东西释放出来就能删除表空间了。这个过程不能回滚,绝对不要在生产环境或者有用的环境上使用。我不对这个过程执行的结果负任何责任

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn