【那些ORCALE DB可以做到的事】
我们可以在 ORACLE DB 中使用 ORACLE 提供的 dbms_scheduler 设置schedule job, 同时会记录下来每次的运行结果,无需自己log
通过以下语句,可以查看已有的jobs, schedules, programs
SELECT * FROM user_scheduler_jobs; --已有jobs
SELECT * FROM user_scheduler_job_args; --已有参数
SELECT * FROM user_scheduler_job_log;
SELECT * FROM user_scheduler_job_run_details;
SELECT * FROM user_scheduler_programs; --已有programs
SELECT * FROM user_scheduler_schedules; --已有schedules
简单无参数的JOBS, RUN PROCEDURE
DECLARE
num number;
BEGIN
SELECT count(1) INTO num FROM user_scheduler_jobs WHERE job_name = 'XXX_JOBS';
if num > 0 then
dbms_scheduler.drop_job(job_name => 'XXX_JOBS'); --如果已存在先drop再create
end if;
dbms_scheduler.create_job(
job_name => 'XXX_JOBS',
job_type => 'STORED_PROCEDURE',
job_action => 'USER_JOB_PACKAGE.USER_JOB_MAIN_XXX',
number_of_arguments => 0, --没有参数
repeat_interval => 'freq=daily;interval=1;byhour=8;byminute=0;bysecond=0', --每天早上8点run
enabled => TRUE); --设置TRUE定时任务开启
END;
/
带参数的JOBS
DECLARE
num number;
BEGIN
SELECT count(1) INTO num FROM user_scheduler_jobs WHERE job_name = 'XXX_HOURLY_JOBS';
if num > 0 then
dbms_scheduler.drop_job(job_name => 'XXX_HOURLY_JOBS');
end if;
dbms_scheduler.create_job(
job_name => 'XXX_HOURLY_JOBS',
job_type => 'STORED_PROCEDURE',
job_action => 'USER_JOB_PACKAGE.USER_JOB_MAIN_XXX',
number_of_arguments => 6,
repeat_interval => 'freq=HOURLY;interval=1;', --每个小时跑一次
enabled => FALSE); -- create 有参数job时 enabled会报错没有参数设置, 先disabled
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 1,
argument_value => 'USER NAME'
);
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 2,
argument_value => 'ACTION LOG'
);
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 3,
argument_value => 1
);
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 4,
argument_value => null
);
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 5,
argument_value => null
);
dbms_scheduler.set_job_argument_value (
job_name => 'XXX_HOURLY_JOBS',
argument_position => 6,
argument_value => null
);
dbms_scheduler.enable(name => 'XXX_HOURLY_JOBS'); -- 最后enable job
END;
/
以上是采用JOBS直接定时运行, 下面介绍使用Schedule + Programs + Jobs 设置定时任务
-- CREATE PROGRAM 指定需要跑的PROCEDURE
DECLARE
num number;
BEGIN
SELECT count(1) INTO num FROM user_scheduler_programs WHERE program_name = 'TEST_PROGRAMS';
if num > 0 then
dbms_scheduler.drop_program(program_name => 'TEST_PROGRAMS',force => TRUE);
end if;
dbms_scheduler.create_program( program_name => 'TEST_PROGRAMS',
program_type => 'STORED_PROCEDURE',
program_action => 'TEST_PACKAGE.TEST_MAIN_PROCEDURE',
number_of_arguments => 0,
enabled => TRUE,
comments => 'Test for setup programs');
END;
/
-- CREATE SCHEDULE 设置时间
DECLARE
num number;
BEGIN
SELECT count(1) INTO num FROM user_scheduler_schedules WHERE schedule_name = 'TEST_SCHEDULE';
if num > 0 then
dbms_scheduler.drop_schedule(schedule_name => 'TEST_SCHEDULE');
end if;
dbms_scheduler.create_schedule(schedule_name => 'TEST_SCHEDULE',
repeat_interval => 'freq=daily;interval=1;byhour=8;byminute=0;bysecond=0');
END;
/
-- OK 将之前CREATE的 SCHEDULE,PROGRAM 放入 JOBS中
DECLARE
num number;
BEGIN
SELECT count(1) INTO num FROM user_scheduler_jobs WHERE job_name = 'TEST_JOBS';
if num > 0 then
dbms_scheduler.drop_job(job_name => 'TEST_JOBS');
end if;
dbms_scheduler.create_job(job_name => 'TEST_JOBS',
program_name => 'TEST_PROGRAMS', -- 指定之前定义的 program
schedule_name => 'TEST_SCHEDULE'); -- 指定之前定义的 schedule
END;
/
Reference:
ORACLE DOCS - DBMS_SCHEDULER