Doc ID: Note:208599.1 dans le METALINK
TIP: Click help for a detailed explanation of this page.
Bookmark Go to End
Subject: How to submit a job with DBMS_JOB.SUBMIT from a trigger
Doc ID: Note:208599.1 Type: HOWTO
Last Revision Date: 25-OCT-2005 Status: PUBLISHED
PURPOSE
-------
This article shows an example on how to submit a job from a
trigger.
SCOPE & APPLICATION
-------------------
This article applies to Oracle employees and customers alike.
HOW TO SUBMIT A JOB WITH DBMS_JOB.SUBMIT from a trigger
--------------------------------------------------------
Trying to submit a job with dbms_job.submit from a trigger may have
given you some errors.
Problems encountered:
- The jobs submit but do execute automatically
(solution: make sure you have a commit after the dbms_job.submit)
- ORA-04092: cannot COMMIT in a trigger
(solution: put the commit in a wrapper procedure and make the
trigger autonomous)
Here is a working example:
This script assumes that you have created the scott/tiger account
with utlsampl.sql.
The script submits the job with a delay of 5 minutes, so that you
have ample time to check on DBA_JOBS and see the job there. Once
executed, check emp_audit for the audit record to be written.
connect / as sysdba
alter system set job_queue_processes = 2;
connect scott/tiger
drop table Triggertab;
drop table emp_audit;
drop trigger triggertrig;
create table triggertab as select * from emp;
create table emp_audit (DT varchar(24));
Create or replace procedure run_job(ProcName Varchar2) as
Jobid Number := 0;
ProcNm Varchar2(30);
Begin
ProcNm := 'Begin '||ProcName||'; End;';
Dbms_job.Submit(Jobid,ProcNm,Sysdate+5/1440,null);
Commit;
End;
/
Create or Replace Procedure Update_audit As
Begin
Insert into emp_audit values (to_char(sysdate, 'HH:MI:SS'));
Commit;
End;
/
CREATE TRIGGER triggertrig
AFTER UPDATE OF sal ON triggertab
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
run_job ('update_audit');
END;
/
update triggertab set sal = sal+1 where rownum=1;
commit;
RELATED DOCUMENTS
-----------------
Note 65961.1 Oracle8i: Autonomous Transactions
Partager