Oracle日志定期清理存储过程 |
发布时间: 2012/8/20 17:24:53 |
常要Oracle数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作,这时需要用到一个函数dbms_job.submit,来完成Oracle定时器Job时间的处理上。使用dbms_job.submit这个函数,我们只需要考虑两个事情:安排某一任务,和定制一个执行任务的时间点。但最重要也是最棘手的事情,我认为还是确定一个执行任务的时间点。时间点确定了,其他的事情就好办了。下面是函数dbms_job.submit使用方法: Java代码 <!--[if !supportLists]-->1. <!--[endif]-->dbms_job.submit( job out binary_integer, <!--[if !supportLists]-->2. <!--[endif]-->what in archar2, <!--[if !supportLists]-->3. <!--[endif]-->next_date in date, <!--[if !supportLists]-->4. <!--[endif]-->interval in varchar2, <!--[if !supportLists]-->5. <!--[endif]-->no_parse in boolean)
1.TRUNC(for dates) 2.确定执行时间间隔 Interval => TRUNC(next_day(sysdate,'星期一'))+2/24
Java代码 <!--[if !supportLists]-->1. <!--[endif]--> <!--[if !supportLists]-->2. <!--[endif]--> SQL> create table test(id number,cur_time date); <!--[if !supportLists]-->3. <!--[endif]--> 表已创建。 <!--[if !supportLists]-->4. <!--[endif]-->----建sequence <!--[if !supportLists]-->5. <!--[endif]-->CREATE SEQUENCE test_sequence <!--[if !supportLists]-->6. <!--[endif]-->INCREMENT BY 1 -- 每次加几个 <!--[if !supportLists]-->7. <!--[endif]--> START WITH 1 -- 从1开始计数 <!--[if !supportLists]-->8. <!--[endif]--> NOMAXVALUE -- 不设置最大值 <!--[if !supportLists]-->9. <!--[endif]--> NOCYCLE -- 一直累加,不循环 <!--[if !supportLists]-->10. <!--[endif]--> CACHE 10 ;
--建触发器代码为: Java代码 <!--[if !supportLists]-->1. <!--[endif]-->create or replace trigger tri_test_id <!--[if !supportLists]-->2. <!--[endif]--> before insert on test --test 是表名 <!--[if !supportLists]-->3. <!--[endif]--> for each row <!--[if !supportLists]-->4. <!--[endif]-->declare <!--[if !supportLists]-->5. <!--[endif]--> nextid number; <!--[if !supportLists]-->6. <!--[endif]-->begin <!--[if !supportLists]-->7. <!--[endif]--> IF :new.id IS NULLor :new.id=0 THEN --id是列名 <!--[if !supportLists]-->8. <!--[endif]--> select test_sequence.nextval --SEQ_ID正是刚才创建的 <!--[if !supportLists]-->9. <!--[endif]--> into nextid <!--[if !supportLists]-->10. <!--[endif]--> from sys.dual; <!--[if !supportLists]-->11. <!--[endif]--> :new.id:=nextid; <!--[if !supportLists]-->12. <!--[endif]--> end if; <!--[if !supportLists]-->13. <!--[endif]-->end tri_test_id; <!--[if !supportLists]-->14. <!--[endif]-->
Java代码 <!--[if !supportLists]-->1. <!--[endif]-->SQL> create or replace procedure proc_test as <!--[if !supportLists]-->2. <!--[endif]--> begin <!--[if !supportLists]-->3. <!--[endif]--> insert into test(cur_time) values(sysdate); <!--[if !supportLists]-->4. <!--[endif]--> end; <!--[if !supportLists]-->5. <!--[endif]--> / <!--[if !supportLists]-->6. <!--[endif]-->
Java代码 <!--[if !supportLists]-->1. <!--[endif]-->SQL> declare job1 number; <!--[if !supportLists]-->2. <!--[endif]--> begin <!--[if !supportLists]-->3. <!--[endif]--> dbms_job.submit(job1,'proc_test;',sysdate,'sysdate+1/1440');--每天1440分钟,即一分钟运行test过程一次 <!--[if !supportLists]-->4. <!--[endif]--> end;
----------------------------------------------
CREATE OR REPLACE PROCEDURE delhisdata AS BEGIN INSERT INTO test_his SELECT * FROM test WHERE ins_date < trunc(add_months(SYSDATE, -12)); DELETE FROM test t WHERE ins_date < trunc(add_months(SYSDATE, -12)); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END; / --1、数据库中建立一个JOB对存储过程进行调用,并且每月执行一次, DECLARE jobno NUMBER; BEGIN DBMS_JOB.SUBMIT(JOB => jobno, /*自动生成JOB_ID*/ WHAT => 'delhisdata;', /*需要执行的过程或SQL语句*/ NEXT_DATE => TRUNC(SYSDATE + 1) + 2 / 24, /*初次执行时间*/ INTERVAL => 'TRUNC(add_months(SYSDATE,1))+2/24'); /*执行周期*/ COMMIT; END; / 本文出自:亿恩科技【www.enkj.com】 |