博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle定时器定时清理某张表指定日期前的数据
阅读量:4036 次
发布时间:2019-05-24

本文共 1459 字,大约阅读时间需要 4 分钟。

--创建存储过程,检查表存不存在,存在就drop掉。 个人认为drop没有mysql方便

create or replace procedure tableExistThenDrop(tablename in varchar2) authid current_user

as
tempsql varchar2(2000);
vflag integer;
begin
  vflag:=0;
  tempsql:='select count(*) from user_tables where table_name = '''||UPPER(tablename)||'''';
  execute immediate tempsql into vflag;
  if vflag = 1 then 
    tempsql:='drop table '||UPPER(tablename);
    execute immediate tempsql;
  end if;
end;
/

--创建存储过程删除某个表中某一天之前的数据

create or replace procedure proce_delvertable authid current_user
as
tempsql varchar2(3000);
temp_date varchar2(20);
begin
  temp_date:=to_char(sysdate,'yyyy-MM-dd');
  
  tableExistThenDrop('user_bak');
  tempsql:='create table user_bak nologging as select * from user';
  execute immediate tempsql;
  
  tempsql:='truncate table user';
  execute immediate tempsql;
  
  tempsql:='insert into user select * from user_bak where dctime >='''||temp_date||' 00:00:00''';
  execute immediate tempsql;
  commit;
end;
/

--用户授权,如果不授权,DDL语句在job中执行不了

Grant Create Table,Create sequence, Create trigger, 
Create procedure, Drop any sequence, Create public synonym, 
Drop public synonym to scott;

--创建job,每周一早上8点执行

declare
  job number;
  BEGIN
     sys.dbms_job.submit(job => job,
                    what => 'proce_delvertable;',
                    next_date => TRUNC(next_day(sysdate,'星期一'))+8/24,
                    interval => 'TRUNC(next_day(sysdate,''星期一''))+8/24');
    COMMIT;

    DBMS_JOB.RUN(job);

  end;

Tips:在这个方案中,使用的是create table as select 的方式进行备份的,如果原表数据量大建议用此备份,因为如果直接delete原表,可能会造成锁表导致正常业务阻塞,而且这种方式的速度远比delete快。

转载地址:http://vqcdi.baihongyu.com/

你可能感兴趣的文章
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
Intellij IDEA启动优化,让开发的感觉飞起来
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
如何优雅的编程,lombok你怎么这么好用
查看>>
一文看清HBase的使用场景
查看>>
除了负载均衡,Nginx还可以做很多,限流、缓存、黑白名单
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>