PL/SQL DBMS_JOB.CHANGE

Oracle’s DBMS_JOB.CHANGE procedure is part of the DBMS_JOB package, which manages scheduled jobs within the Oracle database. Before diving into DBMS_JOB.CHANGE, it’s important to understand the context in which it operates.

The DBMS_JOB package allows database administrators and developers to schedule, run, and manage jobs that execute various database tasks. These tasks can range from routine maintenance operations like statistics gathering and data purging to application-specific business logic. The package provides a simple yet powerful way to automate database tasks, ensuring they are executed at appropriate times without manual intervention.

Understanding DBMS_JOB.CHANGE

The DBMS_JOB.CHANGE procedure is used to modify the characteristics of an existing job in the job queue. With this procedure, you can change the job’s executable PL/SQL block or the job’s execution schedule. This flexibility is crucial for adapting to changing business requirements or operational environments.

Syntax

The basic syntax for DBMS_JOB.CHANGE is as follows:

DBMS_JOB.CHANGE (
   job        IN NUMBER,
   what       IN VARCHAR2,
   next_date  IN DATE,
   interval   IN VARCHAR2,
   instance   IN NUMBER DEFAULT NULL,
   force      IN BOOLEAN DEFAULT FALSE);

job: The job number assigned by Oracle when the job was submitted. This is how you identify which job you’re changing.
what: The PL/SQL block of code that the job will execute. You can change the job’s functionality by modifying this block.
next_date: Specifies the next date and time when the job should be run. Changing this allows you to reschedule the job.
interval: A VARCHAR2 expression that evaluates to the next date when the job should run after next_date. This allows for repeating jobs based on dynamic calculations.
instance: This parameter is related to Oracle Real Application Clusters (RAC) environments and specifies which instance the job should run on. If NULL, the job can run on any instance.
force: A boolean parameter that, if set to TRUE, allows changing a job even if it’s being run by another session.

Usage Example

Imagine you have a job that gathers table statistics nightly, but you need to change it to run weekly. Here’s how you might use DBMS_JOB.CHANGE to modify the job’s schedule:

BEGIN
   DBMS_JOB.CHANGE(
      job => 42,
      what => 'BEGIN DBMS_STATS.GATHER_SCHEMA_STATS(ownname => ''MY_SCHEMA''); END;',
      next_date => SYSDATE + 7,
      interval => 'SYSDATE + 7');
END;
/

This changes the job with job number 42 to gather schema statistics and then reschedules it to run weekly, starting from the current date.

Considerations

Version Compatibility: The DBMS_JOB package has been superseded by the more robust and feature-rich DBMS_SCHEDULER in later versions of Oracle. While DBMS_JOB is still supported for backward compatibility, Oracle recommends using DBMS_SCHEDULER for new applications.

Permissions: Executing DBMS_JOB.CHANGE requires specific privileges that are typically held by database administrators.

The Oracle DBMS_JOB.CHANGE is a powerful tool for modifying the behavior and schedule of database jobs, allowing for dynamic adjustments to meet operational needs. However, with the introduction of DBMS_SCHEDULER, it’s important to consider using the newer package for enhanced functionality and flexibility in managing database jobs.