查看全局的定时任务
SELECT * FROM DBA_SCHEDULER_JOBS ;
查看当前帐号的定时任务:
SELECT * FROM USER_SCHEDULER_JOBS ;
定义存储过程:
CREATE OR REPLACE PROCEDURE aaaaa
IS
BEGIN
UPDATE bbbbb SET STATUS = 1 WHERE STATUS = 0 AND OBJECTCREATETIME < CURRENT_DATE;
END CLEANUP_XTPT_SYNC_OLDDATA;
设置定时任务:
begin
sys.dbms_scheduler.create_job(job_name => 'aaaaa_job',
job_type => 'STORED_PROCEDURE',
job_action => 'aaaaa',
start_date => to_date('2023-1-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
repeat_interval => 'Freq=Daily;BYHOUR=23;BYMINUTE=45;BYSECOND=0',
end_date => to_date(null),
job_class => 'DEFAULT_JOB_CLASS',
enabled => true,
auto_drop => false,
comments => '说明');
end;
设置定时任务的说明:
begin
sys.dbms_scheduler.create_job(job_name => 'cleanup_xtpt_sync_olddata_job', --定时任务名称
job_type => 'STORED_PROCEDURE', --job类型 --可以是 STORED_PROCEDURE(存储过程)、PLSQL_BLOCK(PL/SQL块)、EXECUTABLE(外部程序)、CHAIN
job_action => '存储过程名称',
start_date => to_date('30-12-2018 01:00:00', 'dd-mm-yyyy hh24:mi:ss'), --job开始生效日期
repeat_interval => 'Freq=Daily;Interval=1;BYHOUR=9;BYMINUTE=30;BYSECOND=0', --执行计划具体时间,这里意思是:每天9:30分执行一次,Interval时间间隔可省略;
end_date => to_date(null), --job失效日期
job_class => 'DEFAULT_JOB_CLASS',
enabled => true, --job启用(enable)/禁用(disable)
auto_drop => false, --job禁用后是否自动删除
comments => '定时任务备注');
end;
周期:Freq关键字用来指定间隔的时间周期,可选参数有:YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY,分别表示年、月、周、日、时、分、秒等单位
INTERVAL 关键字用来指定间隔的频繁,可指定的值的范围从1-99
一些操作:
禁用:
BEGIN
DBMS_SCHEDULER.DISABLE(name => 'aaaaa_job');
END;
强制禁用:
BEGIN
DBMS_SCHEDULER.DISABLE(name => 'aaaaa_job',force=>true);
END;
启用:
BEGIN
DBMS_SCHEDULER.ENABLE(name => 'aaaaa_job');
END;
删除
exec dbms_scheduler.drop_job('aaaaa_job')
强制删除:
exec dbms_scheduler.drop_job(job_name=>'aaaaa_job',force=>true)
查询执行情况:
select * from dba_SCHEDULER_JOBS where job_name='aaaaa_job'
logs:job执行情况都会记录在log里,这也方便我们去监控job执行情况