Sunteți pe pagina 1din 4

Oracle Database provides scheduling capabilities with inbuilt Oracle Job

Scheduler. Oracle has the DBMS_SCHEDULER package which provides a


collection of scheduling functions and procedures that are callable from any
PL/SQL program. We can even use SQL Developer IDE to create and schedule
jobs.
In this 2 part article we will see everything about scheduling in Oracle. We
will see examples and illustration code to understand things better.

Tools Used:
 Oracle Database Express Edition 11g Release 2
 SQL Developer 4.2
In first part of the article we will briefly visit the components used to
schedule job and what are the prerequisites that needs to be done before
any of the component is created.

Job
A job is the combination of a schedule and a program, along with any
additional arguments required by the program. A schedule is nothing but the
frequency of job execution. A job can perform a task that is defined by one of
these –

 PL/SQL block
 Stored Procedure
 Remote Stored Procedure
 Chain
 Program
 Executable
Program
A program is a collection of metadata about a particular task. The programs
are not schedulable on their own. You need to define program using one of –

 PL/SQL block
 Stored Procedure
 Remote Stored Procedure
 Executable
Chain
A chain is a series of programs that are linked together to perform a
particular group of activities. To create a chain, you need to follow below
steps –
 Create a chain
 Define the steps in the chain
 Add rules
 Enable (If you are using SQL Developer, you can skip this step as you
have option to enable chain at creation time.)
 Create a job that runs this chain as scheduled.
Prerequisites
To create a job, program and chains we need to grant our user some
privileges. To do that connect to database using sql plus command line and
execute below code blocks. You need to connect as SYSDBA

Prerequisites Grants
Oracle PL/SQL

1 BEGIN
2 GRANT CREATE ANY JOB TO <USER NAME>;
3 GRANT EXECUTE ON DBMS_SCHEDULER TO <USER NAME>;
4 GRANT MANAGE SCHEDULER TO <USER NAME>;
5 END;
6  
7 BEGIN
8 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(DBMS_RULE_ADM.CREATE_RULE_OBJ, '<USER
9 NAME>');
1 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(DBMS_RULE_ADM.CREATE_RULE_SET_OBJ, '<USER
0 NAME>');
1 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(DBMS_RULE_ADM.CREATE_EVALUATION_CONTEXT_O
1 BJ, '<USER NAME>');
1 END;
2 /

Illustration
For illustrations purpose we have defined 2 tables and 3 procedures. Brief
description about them is as

 JOB_PARAMETERS – To store some intermediate parameter and


corresponding values
 JOB_LOG – To store logging statements.
 PROCEDURE JOB_PROC_STEP_1 – This procedure will create a random
file name and put it in table job_parameters
 PROCEDURE JOB_PROC_STEP_2 – This procedure will create a file using
file name generated in above step.
 PROCEDURE JOB_PROC_STEP_3 – This procedure will update
job_parameters table and mark the parameter as archived.
The code for above illustrations can be downloaded from our GIT Repository
So far we have covered the basics of Oracle Job Scheduler now lets see it in
action. In this part we will go through example of creating a job using stored
procedure.

Oracle Job Scheduler – Create a Job with


Stored Procedure
As mentioned earlier, we will create a job stored procedure. We are using
SQL Developer to create our Job. Depending upon type of job you choose to
create it will prompt you define the parameters. E.g. if you choose to create
PL/SQL block it will give you space to write your code. If you choose stored
procedure, then it will list down all the procedures available for that user and
you can select any one of that to be scheduled as job.

Lets see each of the steps for same

1. In the sql developer right click on Job and select New Job (Wizard)
2. You will see below screen. Specify name and description.
3. Keep Job class as default.

4. Select the “Repeating” from when to execute job


5. Click on the pencil icon, it will open up another window where you can
specify when this job will be executed. Set the frequency as desired and
click ok.
6. You can see above how the interval is set for this job to be executed
every 15 minutes.

S-ar putea să vă placă și