ORACLE小工具:存储过程清空所有表或使所有触发器失效

清空所有表:

CREATE OR REPLACE PROCEDURE CLEAN_TABLES as
  v_tablename varchar2(256);
  cursor cur_tablename is
    select object_name from user_objects where object_type = 'TABLE';
begin
  open cur_tablename;
  loop
    fetch cur_tablename
      into v_tablename;
    exit when cur_tablename%notfound;
    execute immediate 'delete from ' || v_tablename || '';
  end loop;
  close cur_tablename;
end CLEAN_TABLES;

运行:

begin
  CLEAN_TABLES();
end;

失效所有触发器:

CREATE OR REPLACE PROCEDURE DISABLE_TRIGGER as
  v_sql varchar2(100);
  v_ref sys_refcursor;
begin
  for v_ref in (select object_name
                  from user_objects
                 where object_type = 'TRIGGER') loop
    v_sql := 'alter trigger ' || v_ref.object_name || 'disable';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
  end loop;
exception
  when others then
    dbms_output.put_line(SQLCODE || '---' || SQLERRM);
end DISABLE_TRIGGER;

生效所有触发器:

CREATE OR REPLACE PROCEDURE ENABLE_TRIGGER as
  v_sql varchar2(100);
  v_ref sys_refcursor;
begin
  for v_ref in (select object_name
                  from user_objects
                 where object_type = 'TRIGGER') loop
    v_sql := 'alter trigger ' || v_ref.object_name || 'enable';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
  end loop;
exception
  when others then
    dbms_output.put_line(SQLCODE || '---' || SQLERRM);
end ENABLE_TRIGGER;

that's it

posted @ 2016-10-24 14:53  但行好事-莫问前程  阅读(924)  评论(1编辑  收藏  举报