Timed tasks in Oracle

1. An example of a scheduled task in Oracle

1.1. Create a test table with only one DATE type field

CREATE TABLE TEST_A(TEST_ADD_DATA DATE);

1.2. Create a custom stored procedure

CREATE OR REPLACE PROCEDURE TEST_PRO AS
BEGIN
    INSERT INTO TEST_A VALUES (SYSDATE);
END;

1.3. Create a scheduled task JOB

DECLARE
    jobno NUMBER;
BEGIN
    dbms_job.submit(
            jobno, --定时器ID,系统自动获得
            'TEST_PRO;', --what执行的过程名
            SYSDATE, --next_date,定时器开始执行的时间,这样写表示立即执行
            'TRUNC(sysdate,''mi'') + 1/ (24*60)' --interval,设置定时器执行的频率,这样写每隔1分钟执行一次
        );
    COMMIT;
END;

illustrate:

1. The jobno after declare is a number type, the main purpose is to save the id for the timer, and the following job refers to this jobno.

2. job: output variable, which is the number of this task in the task queue;

3. what: the name of the executed task and its input parameters;

4. next_date: the time of task execution;

5. interval: the time interval for task execution.

1.4. Run JOB (after 1.3 is successfully executed, the job has already started to execute)

select * from TEST_A;

1.5. Query operation about JOB

-- 查看调度任务
select * from user_jobs;

-- 查看正在执行的调度任务
select * from dba_jobs_running;

-- 查看执行完的调度任务
select * from dba_jobs;

The id of the job, this id is not filled in casually, but executes select * from user_jobs; to query the id corresponding to the scheduled task name.

Manually execute the scheduled task (parameter 25 below needs to be determined according to the parameters of the local query)

BEGIN
    DBMS_JOB.RUN(25);
    COMMIT;
END;

Stop a cron job that has started

BEGIN
    DBMS_JOB.BROKEN(25,  TRUE,  SYSDATE);
    COMMIT;
END;

Delete the specified job

BEGIN
    DBMS_JOB.REMOVE(25);
    commit;
END;

1.6. Common Interval settings

描述                        INTERVAL参数值 
每天午夜12点                 TRUNC(SYSDATE + 1) 
每天早上830分              TRUNC(SYSDATE + 1) +8*60+30/(24*60) 
每星期二中午12点              NEXT_DAY(TRUNC(SYSDATE ), ''TUESDAY'' ) + 12/24 
每个月第一天的午夜12点         TRUNC(LAST_DAY(SYSDATE ) + 1) 
每个季度最后一天的晚上11点      TRUNC(ADD_MONTHS(SYSDATE + 2/24, 3 ), 'Q' ) -1/24 
每星期六和日早上610分        TRUNC(LEAST(NEXT_DAY(SYSDATE, ''SATURDAY"), NEXT_DAY(SYSDATE, "SUNDAY"))) +6×60+10/24×60)
每秒钟执行次
 
Interval => sysdate + 1/(24 * 60 * 60)
 
如果改成sysdate + 10/(24 * 60 * 60)就是10秒钟执行次
 
每分钟执行 
Interval => TRUNC(sysdate,'mi') + 1/ (24*60)
 
如果改成TRUNC(sysdate,'mi') + 10/ (24*60) 就是每10分钟执行次
 
每天定时执行 
例如:每天的凌晨1点执行 
Interval => TRUNC(sysdate) + 1 +1/ (24)
 
每周定时执行 
例如:每周一凌晨1点执行 
Interval => TRUNC(next_day(sysdate,'星期一'))+1/24
 
每月定时执行 
例如:每月1日凌晨1点执行 
Interval =>TRUNC(LAST_DAY(SYSDATE))+1+1/24
 
每季度定时执行 
例如每季度的第一天凌晨1点执行 
Interval => TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 1/24
 
每半年定时执行 
例如:每年71日和11日凌晨1Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+1/24
 
每年定时执行 
例如:每年11日凌晨1点执行 
Interval =>ADD_MONTHS(trunc(sysdate,'yyyy'),12)+1/24

1.7. Writing a format example of a stored procedure

-- oracle存储过程格式
CREATE OR REPLACE PROCEDURE TEST_PRO_NAME AS
    declare countNum INT :=0;
    begin
        select count(*) into SM_USER from test2;
        if(countNum=0) then
            --delete from Table
            --INSERT INTO table (col1, col2, col3)
            --SELECT col1,col2,col3 FROM table WHERE condition;

            --业务完成写日志
        else
            --没有数据写日志
     end if;
        commit;
    Exception When Others Then
        rollback;

END TEST_PRO_NAME

Guess you like

Origin blog.csdn.net/KevinChen2019/article/details/128256517