The Oracle package dbms_job can be used to schedule jobs. The procedure dbms_job.submit() accepts three parameters:
- the name of the job to submit
- the start time for the job
- the interval to execute the job
Example
DBMS_JOB.SUBMIT (
job => :jobno
, what => 'statspack.snap;' -- What to run
, next_date => TRUNC(sysdate+1/24,'HH') -- Start next hour
, interval => 'TRUNC(SYSDATE+1/24,''HH'')' -- Run every hour
);
SQL> VARIABLE jobno NUMBER;
Submit job to begin at 0600 and run every hour
SQL> EXEC DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;' , TRUNC(sysdate+1)+6/24, 'TRUNC(SYSDATE+1/24,''HH'')' );
Submit job to begin at 0900 and run 12 hours later
SQL> EXEC DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;' , TRUNC(sysdate+1)+9/24 , 'TRUNC(SYSDATE+12/24,''HH'')' );
Submit job to begin at 0600 and run every 10 minutes
SQL> EXEC DBMS_JOB.SUBMIT (:jobno , 'statspack.snap;' , TRUNC(sysdate+1)+6/24, 'TRUNC(sysdate+10/1440,''MI'')' );
Submit job to begin at 0600 and run every hour, Monday - Friday
SQL > EXEC DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;', TRUNC(sysdate+1)+6/24 , 'decode(to_char(sysdate+1,''Dy''), 'SAT', sysdate+3, ''SUN'', sysdate+2,sysdate+1)' );
Job execution intervals are determined by the date expression set by the interval parameter. Two different types of jobs can be scheduled
- Each execution of the job should follow the last by a specific time interval.
- The job should execute on specific dates and times.
Job Intervals and Date Arithmetic
Type 1 Jobs
Jobs of type 1 usually have relatively simple date arithmetic expressions
of the type SYSDATE+N, where N represents the time interval
expressed in days. The following table provides examples of these types of intervals.
The job intervals only expresses that the spacing between executions will be at least that specified.
For instance, if a job is first executed at 12:00 p.m. with in
interval of 'SYSTEM + 1', it will be scheduled to execute the next day at 12:00 p.m.
However, if a user executes the job manually at 4:00 p.m. the next
day using DBMS_JOB.RUN, then it will be rescheduled for execution at 4:00 p.m. the next day.
If the database is down or the job queue so busy that the job cannot be executed exactly at the time scheduled then the job
will run as soon as it can, but the execution time will be different.
For Type I jobs with jobs with simple interval expressions this "drift" in next execution times is characteristic.
|