How to Schedule A Job In Oracle Using DBMS_SCHEDULER?

9 minutes read

To schedule a job in Oracle using DBMS_SCHEDULER, you need to follow the steps mentioned below:

  1. Verify Privileges: Ensure that you have the necessary privileges to create and manage scheduler objects. Typically, you will need the "CREATE JOB" privilege and appropriate roles.
  2. Create a Program: A program defines the executable and its attributes. Use the DBMS_SCHEDULER.CREATE_PROGRAM procedure to create a program, specifying its name, type (usually 'STORED_PROCEDURE' for a PL/SQL procedure), and other relevant attributes.
  3. Create a Schedule: A schedule determines when and how often the job should execute. You can create different types of schedules based on your requirements, such as daily, weekly, monthly, or even customized schedules. Use the DBMS_SCHEDULER.CREATE_SCHEDULE procedure to create a schedule, specifying its name, type, and any additional attributes.
  4. Create Job and Associate Program: Associate a program and schedule with a job by using the DBMS_SCHEDULER.CREATE_JOB procedure. Specify the name of the job, the program to be executed, the schedule for the job, and any other relevant attributes.
  5. Optional: Set Job Arguments or Credentials: If your program requires specific arguments or credentials to execute, you can set them using procedures like DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE or DBMS_SCHEDULER.SET_JOB_CREDENTIAL.
  6. Enable the Job: Once the job is created, enable it using the DBMS_SCHEDULER.ENABLE procedure. This ensures that the job will be scheduled and executed as per the defined schedule.
  7. Monitor the Job: You can monitor the job's status and view its execution history by querying the relevant DBA_SCHEDULER views or using DBMS_SCHEDULER procedures like DBMS_SCHEDULER.GET_JOB_STATUS or DBMS_SCHEDULER.GET_JOB_RUN_DETAILS.
  8. Modify or Delete the Job: If needed, you can modify the job's attributes using procedures like DBMS_SCHEDULER.SET_ATTRIBUTE or delete the job using DBMS_SCHEDULER.DROP_JOB.


It's important to note that the above steps provide a high-level overview of scheduling a job in Oracle using DBMS_SCHEDULER. Depending on your specific requirements, you may need to explore additional features and configurations provided by Oracle's scheduler.

Best Oracle Books to Read of July 2024

1
Oracle PL/SQL by Example (The Oracle Press Database and Data Science)

Rating is 5 out of 5

Oracle PL/SQL by Example (The Oracle Press Database and Data Science)

2
Oracle Database 12c DBA Handbook (Oracle Press)

Rating is 4.9 out of 5

Oracle Database 12c DBA Handbook (Oracle Press)

3
Oracle Database Administration: The Essential Refe: A Quick Reference for the Oracle DBA

Rating is 4.8 out of 5

Oracle Database Administration: The Essential Refe: A Quick Reference for the Oracle DBA

4
Oracle DBA Mentor: Succeeding as an Oracle Database Administrator

Rating is 4.7 out of 5

Oracle DBA Mentor: Succeeding as an Oracle Database Administrator

5
OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

Rating is 4.6 out of 5

OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

6
Oracle Database 12c SQL

Rating is 4.5 out of 5

Oracle Database 12c SQL

7
Oracle Autonomous Database in Enterprise Architecture: Utilize Oracle Cloud Infrastructure Autonomous Databases for better consolidation, automation, and security

Rating is 4.4 out of 5

Oracle Autonomous Database in Enterprise Architecture: Utilize Oracle Cloud Infrastructure Autonomous Databases for better consolidation, automation, and security


How to enable a scheduled job in Oracle DBMS_SCHEDULER?

To enable a scheduled job in Oracle DBMS_SCHEDULER, you can use the ENABLE() procedure. Here are the steps:

  1. Connect to your Oracle database using a SQL client or command-line interface.
  2. Access the Oracle Scheduler using the following command:
1
EXECUTE DBMS_SCHEDULER.set_scheduler_attribute('SCHEDULER_DISABLED', 'FALSE');


This command ensures that the Scheduler is not disabled.

  1. Enable the specific job using the ENABLE() procedure. For example, to enable a job named "my_job", use the following command:
1
EXECUTE DBMS_SCHEDULER.enable('my_job');


Replace "my_job" with the name of your actual job.

  1. If you want to enable multiple jobs, you can run the ENABLE() procedure for each job separately.


You can also enable multiple jobs in a single call using the ENABLE() procedure with a comma-separated list of job names. For example:

1
EXECUTE DBMS_SCHEDULER.enable('job1, job2, job3');


After enabling the scheduled job(s), they will start running according to their defined schedule.


How to pass parameters to a scheduled job using DBMS_SCHEDULER?

To pass parameters to a scheduled job using DBMS_SCHEDULER, you can make use of the job's arguments feature. The following steps outline how to achieve this:

  1. Create a procedure or function that includes the required parameters. This procedure or function will be executed by the scheduled job.
  2. Define the parameters for the scheduled job. To do this, you need to set the job_action parameter of the job to the procedure or function created in the previous step. Use the SET_JOB_ARGUMENT_VALUE procedure of the DBMS_SCHEDULER package to set the desired parameter values.


Here's an example illustrating the steps mentioned above:

  1. Create a procedure that includes parameters: CREATE OR REPLACE PROCEDURE my_procedure(p_param1 VARCHAR2, p_param2 NUMBER) IS BEGIN -- Your logic here NULL; END;
  2. Create a scheduled job and set its parameters: BEGIN DBMS_SCHEDULER.CREATE_JOB( job_name => 'my_scheduled_job', job_type => 'STORED_PROCEDURE', job_action => 'my_procedure', repeat_interval => 'FREQ=DAILY', start_date => SYSTIMESTAMP, enabled => TRUE ); DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE( job_name => 'my_scheduled_job', argument_position => 1, argument_value => 'Hello' ); DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE( job_name => 'my_scheduled_job', argument_position => 2, argument_value => 123 ); END;


In this example, the my_procedure procedure takes two parameters: p_param1 (VARCHAR2) and p_param2 (NUMBER). The CREATE_JOB procedure is used to create the scheduled job named 'my_scheduled_job' with a daily repeat interval. The SET_JOB_ARGUMENT_VALUE procedure is then used to set the values for the parameters in the same order they are declared in the procedure. In this case, 'Hello' is passed as the value for p_param1, and 123 is passed as the value for p_param2.


How to set the repeat interval for a scheduled job in Oracle?

To set the repeat interval for a scheduled job in Oracle, you can use the DBMS_SCHEDULER package with the CREATE_JOB procedure. Here's an example of how to do it:

  1. Connect to your Oracle database using a suitable database user with the necessary privileges.
  2. Use the CREATE_JOB procedure of the DBMS_SCHEDULER package to create a new scheduled job. Here is the syntax:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
BEGIN
  DBMS_SCHEDULER.CREATE_JOB(
    job_name        => 'JOB_NAME',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'PLSQL_CODE',
    start_date      => 'START_DATE',
    repeat_interval => 'INTERVAL',
    end_date        => 'END_DATE',
    enabled         => TRUE
  );
END;
/


Replace the placeholders with the appropriate values:

  • JOB_NAME: The name of your job.
  • PLSQL_BLOCK: The type of job action. It can be 'PLSQL_BLOCK', 'STORED_PROCEDURE', or 'EXECUTABLE'.
  • PLSQL_CODE: The actual PL/SQL code or procedure name to be executed.
  • START_DATE: The date and time when the job should start.
  • INTERVAL: The repeat interval for the job. You can specify it using interval syntax. For example, 'FREQ=DAILY; BYHOUR=8; BYMINUTE=0; BYSECOND=0' will run the job daily at 8:00 AM.
  • END_DATE: The optional end date for the job.
  • TRUE: If you want the job to be enabled initially. Set it to FALSE if you want to enable it later.
  1. Execute the PL/SQL block to create the scheduled job.
  2. Your scheduled job is now created with the specified repeat interval. It will execute according to the specified interval until the end date (if provided) or until you disable or delete it.


Remember to grant the necessary privileges to the database user running the job to access the required objects or perform the necessary operations within the PL/SQL code or stored procedure.

Facebook Twitter LinkedIn Telegram

Related Posts:

To drop a scheduled job in Oracle, you can use the DBMS_SCHEDULER package or the Oracle Enterprise Manager.If using the DBMS_SCHEDULER package, you can use the DROP_JOB procedure to remove a specific scheduled job from the database. You will need to specify th...
To schedule a task, also known as a cron job, in PostgreSQL you can use the pg_cron extension. This extension allows you to schedule and run PostgreSQL commands at specific intervals.First, you need to install the pg_cron extension in your PostgreSQL database....
In Oracle, the equivalent of SQL Profiler is a tool called Oracle Trace or Oracle Trace File Analyzer (TFA). This tool allows users to capture and analyze SQL statements and other activities happening in an Oracle database. It provides detailed information abo...