oracle: job usage

https://www.cnblogs.com/yjmyzz/p/how-to-use-job-in-oracle.html

Oracle's job is actually a scheduled task built into the database, similar to the Timer function in the code. The following is the use process:

Here we simulate a scenario: periodically calling the stored procedure P_TEST_JOB to insert data into the table TEST_JOB_LOG

Table Structure:

Copy code 1 create table TEST_JOB_LOG 2 ( 3 rec_id NUMBER not null, 4 occr_time DATE 5 ); 6 alter table TEST_JOB_LOG 7 add constraint PK_TEST_JOB primary key (REC_ID); Copy code sequence:

Copy code 1 create sequence SEQ_TEST_JOB_LOG 2 minvalue 1 3 maxvalue 99999999 4 start with 1 5 increment by 1 6 cache 10; Copy code stored procedure:

1 create or replace procedure P_TEST_JOB is 2 begin 3 insert into test_job_log(rec_id, occr_time) values(seq_test_job_log.nextval,sysdate); 4 commit; 5 end P_TEST_JOB; The above is just the preparatory work, the following is the key: (The following scripts are all pl /sql developer environment)

1. Create a job

Press Ctrl+C to copy the code

declare job_id number; begin sys.dbms_job.submit(job_id, 'P_TEST_JOB;', sysdate, 'sysdate+1/1440'); -- run immediately, then run every minute sys.dbms_output.put_line(job_id); -- Output job Id end; press Ctrl+C to copy the code After each job is created, it will correspond to a unique number. In the output panel of pl/sql, you can see the job id value output by dbms_output.put_line.

2. Check the running status of the job

1 select * from dba_jobs; -- requires dba permission 2 select * from dba_jobs_running; -- requires dba permission 3 select * from all_jobs;
4 select * from user_jobs; 3. Delete job

begin dbms_job.remove(108); --108 is the specific job ID, which can be obtained by querying select * from user_jobs; 4. Start the job manually

1 begin 2 dbms_job.run(109);--Run the specified Job 3 end; Finally, give a few small examples about creating jobs:

a. Stored procedure call with parameters

Copy code 1 declare 2 job_id number; 3 begin 4 sys.dbms_job.submit(job_id, 'P_JOB_XXX(sysdate-30,sysdate);', sysdate, 'trunc(sysdate+1)+(4 60)/(24 60) '); --Run 5 every morning at 4:00 sys.dbms_output.put_line(job_id); --Output job Id 6 end; Copy code The incoming parameters of P_JOB_CKG are specified as sysdate and sysdate-30, if it is a string Parameter, need to add two single quotes, similar to 'P_XXX(''parameter value'');'

In addition, single quotes can also be replaced by CHR(39), for example:

Copy Code 1 DECLARE 2 JOB_ID NUMBER; 3 BEGIN 4 SYS.DBMS_JOB.SUBMIT(JOB_ID, 5 'P_JOB_XXX(TO_DATE(' || chr(39) || 6 '2014-2-12 00:00:00' || chr (39) || ', ' || chr(39) || 7 'YYYY-MM-DD HH24:MI:SS' || chr(39) || '),TO_DATE(' || 8 chr(39) || '2014-2-12 23:59:59' || chr(39) || ', ' || 9 chr(39) || 'YYYY-MM-DD HH24:MI:SS' || chr( 39) || 10 '));', 11 TO_DATE('2015-1-6 10:00:00', 'YYYY-MM-DD HH24:MI:SS'), 12 'SYSDATE+10/(60* 24)'); --2015-01-06 10:00:00 Start execution, execute every 10 minutes 13 SYS.DBMS_OUTPUT.PUT_LINE(JOB_ID); --Output JOB ID
14 END; Copy the disgusting string in the middle of the code The concatenation is nothing more than to get the string:

P_JOB_XXX(TO_DATE('2014-2-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),TO_DATE('2014-2-12 23:59:59', 'YYYY-MM-DD HH24:MI:SS'));

b. An example of an expression about a specified time

Run 'SYSDATE + 1' once a day

Run 'SYSDATE + 1/24' every hour


Run 'SYSDATE + 10/(60*24)' every 10 minutes

run
'SYSDATE + 30/(60 24 60)' every 30 seconds

run
'SYSDATE + 7' every other week


Run 'TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,1))) + 23/24 on the last day of every month

Every year at 0:00 on January 1st
'TRUNC(LAST_DAY(TO_DATE(EXTRACT(YEAR FROM SYSDATE)||'12'||'01','YYYY-MM-DD'))+1)'

Every day at 12 midnight
'TRUNC(SYSDATE + 1)'

Every morning at 8:30
'TRUNC(SYSDATE + 1) + (8 60+30)/(24 60)'

Every Tuesday at 12 noon
'NEXT_DAY(TRUNC(SYSDATE ), ''TUESDAY'' ) + 12/24'

12 midnight on the first day of every month
'TRUNC(LAST_DAY(SYSDATE ) + 1)'

23:00 on the last day of every month
'TRUNC (LAST_DAY (SYSDATE)) + 23 / 24'

11pm on the last day of each quarter
'TRUNC(ADD_MONTHS(SYSDATE + 2/24, 3 ), 'Q' ) -1/24'

Every Saturday and Sunday at 6:10 am
'TRUNC(LEAST(NEXT_DAY(SYSDATE, ''SATURDAY"), NEXT_DAY(SYSDATE, "SUNDAY"))) + (6 60+10)/(24 60)'

Author: Yang Guo under the Bodhi Tree Source: http://yjmyzz.cnblogs.com The copyright of this article belongs to the author and the blog garden. Reprints are welcome, but this statement must be retained without the author's consent, and the original text should be given in a prominent position on the article page. Connect, otherwise reserve the right to pursue legal responsibility.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325461575&siteId=291194637