Oracle Jobs
Published by : Obay Salah , November 19, 2024
In the Job, we determine what we want to do and also when we want to execute it. As for the question of what we want to do, it can be either:
SQL Statements or PL/SQL Block or PL/SQL Stored Procedure or Java Procedure or Shell Script or Executable File or External Procedure.
As for the question of when we want to execute the task, the execution time of the Job is specified, and the periodic time, i.e., the repetition interval for execution, is also defined.
Generally, there are many options for creating the Job using the CREATE_JOB procedure found in the DBMS_SCHEDULER package.
- JOB_NAME: To specify the name of the Job, which must be unique within the Schema.
- JOB_TYPE: This can be either PLSQL_BLOCK, STORED_PROCEDURE, or EXECUTABLE.
- JOB_ACTION: This variable contains a PL/SQL Block or SQL statement if the JOB_TYPE variable contains the value PLSQL_BLOCK.
If the JOB_TYPE variable has the value STORED_PROCEDURE, then the procedure name must be specified here.
If the JOB_TYPE variable has the value EXECUTABLE, the JOB_ACTION variable can take anything that can be executed through the operating system, such as a file that performs some tasks on the operating system.
- NUMBER_OF_ARGUMENTS: This variable specifies the number of arguments that must be provided in the Job_Action.
- START_DATE: To specify the start time for executing this Job.
- REPEAT_INTERVAL: To determine how this Job's execution will be repeated.
- END_DATE: To specify the end time for executing the Job.
- JOB_CLASS: To specify the class that works to coordinate between Resource & Scheduler Manager.
- ENABLED: To determine whether the Job will be executed or remain inactive until this variable's value is changed to TRUE. By default, this variable is set to FALSE.
- AUTO_DROP: To specify whether the Job will be deleted after its execution period ends (END_DATE) or not. By default, the Job remains in existence by setting this variable's value to TRUE.
- COMMENTS: For writing any comments you wish.
begin dbms_scheduler.create_job( job_name=>'insertserial', job_type=>'plsql_block', job_action=>'insert into tserial values(sysdate);', start_date=>sysdate, repeat_interval=>'sysdate+1', end_date=>sysdate+30, enabled=>true, auto_drop=>true); commit; end;
Thus, we have created a Job named INSERTSERIAL. This Job adds a sequential number to the Tserial table daily, starting from today and continuing for a full month.
This Job runs immediately after its creation and is deleted after its last execution, which is one month later. Generally, the Job is an entity in the database that performs certain tasks.
These tasks can be a Stored Procedure in the database or a PLSQL Block written within the Job, or it can be a file executed through the operating system.
These tasks are executed automatically by specifying the execution time and the execution repetition time.
Notice with me that this procedure creates a self-sufficient Job, meaning it does not connect to a Scheduler or Program.
Generally, the DBMS_SCHEDULER package contains four procedures named CREATE_JOB, each with different properties from the others.
You can inquire about the Jobs that are running in the database through the DBA_SCHEDULER_JOBS table.
From here, you can know the details of the Jobs that are running in the database.
You can enable the disabled Jobs by using the ENABLE procedure available in the DBMS_SCHEDULER package.
BEGIN DBMS_SCHEDULER.ENABLE('INSERTSERIAL'); END; /
You can also disable it using the DISABLE procedure.
BEGIN DBMS_SCHEDULER.DISABLE('INSERTSERIAL'); END; /
The database administrator can delete jobs from the database using the DROP_JOB procedure.
BEGIN DBMS_SCHEDULER.DROP_JOB('INSERTSERIAL',TRUE); END; /
You can also stop a job using the STOP procedure and run a job using the RUN procedure.
Note: When creating a database using DBCA, two jobs are created: the first is PURGE_LOG, which cleans up the Scheduler log, and the second is GATHER_STATS_JOB, which gathers statistics for database analysis.
Comments
no comment yet!