参考文档:
IF: An Example to Convert from DBMS_JOB Jobs to DBMS_SCHEDULER Jobs (Doc ID 2117140.1)
https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SCHEDULER.html#GUID-72D32FB4-11B9-44D1-AFED-11229FC9531F
https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_JOB.html#GUID-7FDE7BB7-F2F2-4305-9A1A-69AD6550FC55
1 获取dbms_job的ddl语句,使用 dbms_job.user_export
set serveroutput on
DECLARE
callstr VARCHAR2(500);
BEGIN
dbms_job.user_export(2, callstr);
dbms_output.put_line(callstr);
END;
/
SYS@test>conn bb/oracle
Connected.
Session altered.
BB@test>
BB@test>
BB@test>set serveroutput on
BB@test>DECLARE
2 callstr VARCHAR2(500);
3 BEGIN
4 dbms_job.user_export(2, callstr);
5 dbms_output.put_line(callstr);
6 END;
7 /
dbms_job.isubmit(job=>2,what=>'pro_insert_date;',next_date=>to_date('30-DEC-21
04.51.08.000000 PM +08:00','YYYY-MM-DD:HH24:MI:SS'),interval=>'trunc(sysdate+1)+
13/24 + 30/1440',no_parse=>TRUE);
PL/SQL procedure successfully completed.
BB@test>
-- 通过查询,和观察实际插入的数据,是每天13:30,执行存储过程一次,也就是每天13:30运行一次job
SQL> select log_user, schema_user, job,next_date,what,interval from dba_jobs where log_user='BB';
LOG_USER SCHEMA_USER JOB NEXT_DATE WHAT INTERVAL
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- ----------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
BB BB 2 2023/8/29 1 pro_insert_date; trunc(sysdate+1)+ 13/24 + 30/1440
SQL>
-- 2 创建dbms_scheduler job ## 每天的13:30 运行一次
BEGIN
DBMS_SCHEDULER.create_job (
job_name => 'sample_procedure_job',
job_type => 'STORED_PROCEDURE',
job_action => 'pro_insert_date',
start_date => TRUNC(SYSDATE)+13/24+30/24/60,
repeat_interval => 'freq=daily; byhour=13; byminute=30',
end_date => NULL,
enabled => TRUE,
comments => 'Job created using the CREATE JOB procedure.');
End;
/
BB@test>BEGIN
2 DBMS_SCHEDULER.create_job (
3 job_name => 'sample_procedure_job',
4 job_type => 'STORED_PROCEDURE',
5 job_action => 'pro_insert_date',
6 start_date => TRUNC(SYSDATE)+13/24+30/24/60,
7 repeat_interval => 'freq=daily; byhour=13; byminute=30',
8 end_date => NULL,
9 enabled => TRUE,
10 comments => 'Job created using the CREATE JOB procedure.');
11 End;
12 /
PL/SQL procedure successfully completed.
BB@test>
--3 确认scheduler job (每天13:30 执行一次)
SQL> select JOB_NAME,JOB_TYPE,JOB_ACTION,STATE,NEXT_RUN_DATE, REPEAT_INTERVAL from dba_scheduler_jobs where job_name='SAMPLE_PROCEDURE_JOB';
JOB_NAME JOB_TYPE JOB_ACTION STATE NEXT_RUN_DATE REPEAT_INTERVAL
-------------------------------------------------------------------------------- ---------------- -------------------------------------------------------------------------------- --------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
SAMPLE_PROCEDURE_JOB STORED_PROCEDURE pro_insert_date SCHEDULED 28-8月 -23 01.30.00.000000 下午 +08:00 freq=daily; byhour=13; byminute=30
SQL>
-- 4 drop掉原来的job
exec dbms_job.remove(2);
BB@test>exec dbms_job.remove(2);
PL/SQL procedure successfully completed.
BB@test>
END