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.laynepeng.cn】 |