The jobs of oralce are very useful.

Source: Internet
Author: User
Tags what parameter

Declare
X number;
Begin
SYS. dbms_job.submit
(Job => X
, What => 'Clare n_errid number; c_errmsg varchar2 (4000); begin pack_sy_dataclear.p_dbdataclear (n_errid, c_errmsg); end ;'
, Next_date => to_date ('26-04-2006 01:00:00 ', 'dd/mm/yyyy hh24: MI: ss ')
, Interval => 'trunc (sysdate + 1 )'
, No_parse => true
);
SYS. dbms_output.put_line ('job number is: '| to_char (x ));
End;
/

Commit;
This script is used to create a job. Note that only one row of SQL statements can be written. Otherwise, compilation fails.

1. Set the initialization parameter job_queue_processes.

SQL> alter system set job_queue_processes = N;(N> 0)
Job_queue_processes: the maximum value is 1000.

View the Job Queue background process
SQL> select name, description from V $ bgprocess;

2. dbms_job package usage
It contains the following sub-processes:

Broken () process.
Change () process.
Interval () process.
Isubmit () process.
Next_date () process.
Remove () process.
Run () process.
Submit () process.
User_export () process.
What () process.

1,
The broken () process updates the status of a submitted job. It is typically used to mark a broken job as a non-broken job.
This process has three parameters: Job, broken, and next_date.

Procedure broken (job in binary_integer,
Broken in Boolean,
Next_date in date: = sysdate)

the job parameter is the work number, which uniquely identifies the work in the problem.
the broken parameter indicates whether the job is marked as broken. -- true indicates that the job is marked as broken, and flase indicates that the job is marked as not broken.
the next_date parameter indicates when the job will run again. The default value of this parameter is the current date and time.
If a job fails to be executed for some reason, Oracle will retry 16 times before the job is successfully executed, A job marked as a broken
restarts in the broken state. There are two methods:
A. Use dbms_job.run () run the job
SQL> begin
SQL> dbms_job.run (: jobno). The jobno is the number of jobs returned when the submit process is submitted.
SQL> end;
SQL>/
B. Use dbms_job.broken () to mark the broken as false again.
SQL> begin
SQL> dbms_job.broken (: Job, false, next_date)
SQL> end;
SQL>/
2. The
change () process is used to change the settings of a specified job.
this process has four parameters: job, what, next_date, and interval.

Procedure change (job in binary_integer,
What in varchar2,
Next_date in date,
Interval in varchar2)

The job parameter is an integer that uniquely identifies the job.
What parameter is a PL/SQL statementCodeBlock.
The next_date parameter indicates when the job will be executed.
The interval parameter indicates the frequency of re-execution.

3,
The interval () process is used to explicitly set the time interval between re-execution of a job.
This process has two parameters: job and interval.

Procedure interval (job in binary_integer,
Interval in varchar2)

The job parameter identifies a specific job. The interval parameter indicates the frequency of re-execution.

4,
The isubmit () process is used to submit a job with a specific job number.
This process has five parameters: job, what, next_date, interval, and no_parse.

Procedure isubmit (job in binary_ineger,
What in varchar2,
Next_date in date,
Interval in varchar2,
No_parse in booean: = false)

The only difference between this process and the submit () process is that this job parameter is passed as an in parameter and includes
The employee ID provided by the developer. If the provided work number is used, an error is generated.

5,
The next_date () process is used to explicitly set the execution time of a job. This process receives two parameters: job and next_date.

Procedure next_date (job in binary_ineger,
Next_date in date)

A job identifies an existing job. The next_date parameter indicates the date and time when the job should be executed.

6,
Remove () to delete a scheduled job. This process receives a parameter:

Procedure remove (job in binary_ineger );

The job parameter uniquely identifies a job. The value of this parameter is the value of the job parameter returned when submit () is called for this job.
A running job cannot be calledProgramDelete.

7,
The run () process is used to immediately execute a specified job. This process only receives one parameter:

Procedure run (job in binary_ineger)

The job that will be executed immediately.

8,
When using the submit () process, the work is properly planned.
This process has five parameters: job, what, next_date, interval, and no_parse.

Procedure submit (job out binary_ineger,
What in varchar2,
Next_date in date,
Interval in varchar2,
No_parse in booean: = false)

The job parameter is the binary_ineger returned by the submit () process. This value uniquely identifies a job.
What parameter is the PL/SQL code block to be executed.
The next_date parameter specifies when the job will be run.
The interval parameter indicates when the job will be re-executed.
The no_parse parameter indicates whether the job should perform syntax analysis at the time of submission or execution -- true
Indicates that this PL/SQL code should perform syntax analysis when it is executed for the first time,
False indicates that the PL/SQL code should be analyzed immediately.

9,
The user_export () process returns a command to schedule an existing job so that the job can be submitted again.
This program has two parameters: job and my_call.

Procedure user_export (job in binary_ineger,
My_call in out varchar2)

The job parameter identifies a scheduled job. The my_call parameter is included in its current state. It is required to resubmit the job.
.

10,
The what () process promises to reset the running command during job execution. This process receives two parameters: job and what.

Procedure what (job in binary_ineger,
What in out varchar2)

The job parameter identifies an existing job. What parameter indicates the new PL/SQL code to be executed.

3. View related job information
1. Related views
Dba_jobs
All_jobs
User_jobs
Dba_jobs_running contains information about running jobs.

2. View related information

SQL> select job, next_date, next_sec, failures, broken
SQL> from dba_jobs;

Job next_date next_sec failures B
---------------------------------
9125 01-jun-01 00:00:00 4 N
14144 24--01 -01 16:35:35 0 n
9127 01-jun-01 00:00:00 16 Y
3 rows selected.

Information about running jobs

Select Sid, R. Job, log_user, R. this_date, R. this_sec
From dba_jobs_running R, dba_jobs J
Where R. Job = J. job;

Sid job log_user this_date this_sec
---------------------------------------------
12 14144 HR 24-oct-94 17:21:24
25 8536 QS 24-oct-94 16:45:12
2 rows selected.

Job Queue lock Information

Select Sid, type, id1, Id2
From v $ lock
Where type = 'jq ';

Sid ty id1 Id2
-----------------------------
12 JQ 0 14144
1 row selected.

Iv. Simple examples

A simple example:

Create test table
SQL> Create Table Test (a date );

The table has been created.

Create a custom Process
SQL> Create or replace procedure myproc
2 begin
3 insert into test values (sysdate );
4 end;
5/

The process has been created.

Create a job
SQL> variable job1 number;
SQL>
SQL> begin
2 dbms_job.submit (: job1, 'myproc; ', sysdate, 'sysdate + 100'); -- 1/1440 minutes a day, that is, one minute to run the test process
3 end;
4/

The PL/SQL process is successfully completed.

Run job
SQL> begin
2 dbms_job.run (: job1 );
3 end;
4/

The PL/SQL process is successfully completed.

SQL> select to_char (A, 'yyyy/MM/DD hh24: MI: ss') time from test;

Time
-------------------
2001/01/07 23:51:21
2001/01/07 23:52:22
2001/01/07 23:53:24

Delete a job
SQL> begin
2 dbms_job.remove (: job1 );
3 end;
4/

The PL/SQL process is successfully completed.
5. Precautions

1. When dbms_job package-related processes such as remove, change, and broken are executed, the login user must be consistent with the user who created the job.

2. When dbms_job.change is executed, if the value of what, next_date and interval is null, the value of the original parameter remains unchanged.
For example
Begin
Dbms_job.change (14144, null, null, 'sysdate + 3 ');
End;
/

3. If you use database link to submit a job, the link must contain the appropriate user name and password.

4. the user who executes the job must have the corresponding permissions for The objects involved in the job process.

5. The database is in restricted mode and cannot execute a job.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.