您的位置:首页 > 数据库

使用dbms_job包来实现数据库后台进程

2006-06-19 10:29 405 查看
1建立实现任务的过程
在schemamanager或SQLPLUS里建立如下过程

CREATEORREPLACEPROCEDURE"CUSTOMER"."T_JOBTEST"as
begin

updateempsetactive=0
whereactive=1
anddate_published<sysdate-active_days;

end;

2向任务队列中加入任务

在SQLPLUS中执行下列script

VARIABLEjobnonumber;
begin
DBMS_JOB.SUBMIT(:jobno,'t_jobtest();',SYSDATE,'SYSDATE+1');
commit;
end;
该任务立即执行(SYSDATE),并且每隔一天执行一次('SYSDATE+1')。

3查询此任务是否加入任务队列

在SQLPLUS中执行下列script

SELECTjob,next_date,next_sec,failures,broken
FROMuser_jobs;

------------------

DBMS_JOB包介绍
调度任务队列里的任务要使用DBMS_JOB包中的过程。使用任务队列不需要特别的数据库特权。任何可以使用这些过程的用户都可以使用任务队列。
Table8-2DBMS_JOB包中的过程

Procedure

Description

Described

SUBMIT
Submitsajobtothejobqueue.向任务队列提交一个任务
REMOVE
Removesaspecifiedjobfromthejobqueue.从任务队列中删除指定的任务
CHANGE
Altersaspecifiedjob.Youcanalterthejobdescription,thetimeatwhichthejobwillberun,ortheintervalbetweenexecutionsofthejob.改变任务
WHAT
Altersthejobdescriptionforaspecifiedjob.改变指定任务的任务内容
NEXT_DATE
Altersthenextexecutiontimeforaspecifiedjob.改变指定任务的下一次执行时间
INTERVAL
Alterstheintervalbetweenexecutionsforaspecifiedjob.改变指定任务的执行时间间隔。
BROKEN
Disablesjobexecution.Ifajobismarkedasbroken,Oracledoesnotattempttoexecuteit.禁止指定任务的执行
RUN
Forcesaspecifiedjobtorun.强制执行指定的任务
SubmittingaJobtotheJobQueue向任务队列提交一个任务
Tosubmitanewjobtothejobqueue,usetheSUBMITprocedureintheDBMS_JOBpackage:
DBMS_JOB.SUBMIT(jobOUTBINARY_INTEGER,whatINVARCHAR2,next_dateINDATEDEFAULTSYSDATE,intervalINVARCHAR2DEFAULT'null',no_parseINBOOLEANDEFAULTFALSE)
TheSUBMITprocedurereturnsthenumberofthejobyousubmitted.Table8-3describestheprocedure'sparameters.

Table8-3DBMS_JOB.SUBMIT的参数

Parameter

Description

job
Thisistheidentifierassignedtothejobyoucreated.Youmustusethejobnumberwheneveryouwanttoalterorremovethejob.Formoreinformationaboutjobnumbers,see"JobNumbers".
what

ThisisthePL/SQLcodeyouwanttohaveexecuted.这里是你想执行的PL/SQL代码
Formoreinformationaboutdefiningajob,see"JobDefinitions".
next_date

Thisisthenextdatewhenthejobwillberun.ThedefaultvalueisSYSDATE.
interval

Thisisthedatefunctionthatcalculatesthenexttimetoexecutethejob.ThedefaultvalueisNULL.INTERVALmustevaluatetoafuturepointintimeorNULL.
Formoreinformationonhowtospecifyanexecutioninterval,see"JobExecutionInterval".
no_parse

Thisisaflag.ThedefaultvalueisFALSE.
IfNO_PARSEissettoFALSE(thedefault),Oracleparsestheprocedureassociatedwiththejob.IfNO_PARSEissettoTRUE,Oracleparsestheprocedureassociatedwiththejobthefirsttimethatthejobisexecuted.If,forexample,youwanttosubmitajobbeforeyouhavecreatedthetablesassociatedwiththejob,setNO_PARSEtoTRUE.
Asanexample,let'ssubmitanewjobtothejobqueue.ThejobcallstheprocedureDBMS_DDL.ANALYZE_OBJECTtogenerateoptimizerstatisticsforthetableDQUON.ACCOUNTS.ThestatisticsarebasedonasampleofhalftherowsoftheACCOUNTStable.Thejobisrunevery24hours:

VARIABLEjobnonumber;begin2>DBMS_JOB.SUBMIT(:jobno,3>'dbms_ddl.analyze_object(''TABLE'',4>''DQUON'',''ACCOUNTS'',5>''ESTIMATE'',NULL,50);'6>SYSDATE,'SYSDATE+1');7>commit;8>end;9>/Statementprocessed.printjobnoJOBNO----------14144

JobDefinition任务定义

任务定义就是SUBMIT过程中WHAT参数中指定的PL/SQL代码。
通常任务定义(内容)是一个过程的一个调用。这个过程能有任意数量的参数。

Note: Inthejobdefinition,usetwosinglequotationmarksaroundstrings.Alwaysincludeasemicolonattheendofthejobdefinition.在任务定义中,用2个单引号包围字符串。任务定义的末尾总是带一个分号。



JobsandImport/Export

Jobscanbeexportedandimported.Thus,ifyoudefineajobinonedatabase,youcantransferittoanotherdatabase.Whenexportingandimportingjobs,thejob'snumber,environment,anddefinitionremainunchanged.
任务是可以被卸出(exported)卸入(imported)的。

JobExecutionInterval任务的执行间隔

TheINTERVALdatefunctionisevaluatedimmediatelybeforeajobisexecuted.Ifthejobcompletessuccessfully,thedatecalculatedfromINTERVALbecomesthenewNEXT_DATE.IftheINTERVALdatefunctionevaluatestoNULLandthejobcompletessuccessfully,thejobisdeletedfromthequeue.
Ifajobshouldbeexecutedperiodicallyatasetinterval,useadateexpressionsimilarto'SYSDATE+7'intheINTERVAL

Ifyoualwayswanttoautomaticallyexecuteajobataspecifictime,regardlessofthelastexecution(forexample,everyMonday),theINTERVALandNEXT_DATEparametersshouldspecifyadateexpressionsimilarto'NEXT_DAY(TRUNC(SYSDATE),"MONDAY")'.

Table8-5listssomecommondateexpressionsusedforjobexecutionintervals.
[b]Table8-5CommonJobExecutionIntervals[/b]
DateExpressionEvaluation
'SYSDATE+7'

exactlysevendaysfromthelastexecution
最后一次执行的7天之后执行
'SYSDATE+1/48'

everyhalfhour
每半个小时执行一次
'NEXT_DAY(TRUNC(SYSDATE),
''MONDAY'')+15/24'

everyMondayat3PM
每个礼拜一的下午3点执行
'NEXT_DAY(ADD_MONTHS(TRUNC(SYSDATE,''Q''),3),
''THURSDAY'')'

firstThursdayofeachquarter每个季度的第一个星期四
Note:
WhenspecifyingNEXT_DATEorINTERVAL,rememberthatdateliteralsandstringsmustbeenclosedinsinglequotationmarks.Also,thevalueofINTERVALmustbeenclosedinsinglequotationmarks.

RemovingaJobfromtheJobQueue删除任务队列中的任务

Toremoveajobfromthejobqueue,usetheREMOVEprocedureintheDBMS_JOBpackage:

DBMS_JOB.REMOVE(jobINBINARY_INTEGER)

Thefollowingstatementremovesjobnumber14144fromthejobqueue:

DBMS_JOB.REMOVE(14144);

SyntaxforWHAT

YoucanalterthedefinitionofajobbycallingtheDBMS_JOB.WHATprocedure.Table8-3describestheprocedure'sparameters.

DBMS_JOB.WHAT(jobINBINARY_INTEGER,
whatINVARCHAR2)



Note:
WhenyouexecuteprocedureWHAT,Oraclerecordsyourcurrentenvironment.Thisbecomesthenewenvironmentforthejob.



SyntaxforNEXT_DATE

YoucanalterthenextdatethatOracleexecutesajobbycallingtheDBMS_JOB.NEXT_DATEprocedure.Table8-3describestheprocedure'sparameters.

DBMS_JOB.NEXT_DATE(jobINBINARY_INTEGER,
next_dateINDATE)


SyntaxforINTERVAL

YoucanaltertheexecutionintervalofajobbycallingtheDBMS_JOB.INTERVALprocedure.Table8-3describestheprocedure'sparameters.

DBMS_JOB.INTERVAL(jobINBINARY_INTEGER,
intervalINVARCHAR2)



ViewingJobQueueInformation查询任务队列的信息



可以通过下表中的数据字典视图来查询任务队列中的任务的信息。

[align=center]Table8-6任务队列信息的视图[/align]

ViewDescription
DBA_JOBSListsallthejobsinthedatabase.列出数据库中的任务
USER_JOBSListsalljobsownedbytheuser.列出该用户拥有的任务
DBA_JOBS_RUNNINGListsalljobsinthedatabasethatarecurrentlyrunning.ThisviewjoinsV$LOCKandJOB$.列出当前运行的任务。


Forexample,youcandisplayinformationaboutajob'sstatusandfailedexecutions.Thefollowingsamplequerycreatesalistingofthejobnumber,nextexecutiontime,failures,andbrokenstatusforeachjobyouhavesubmitted:

显示任务的状态和失败执行的次数。

SELECTjob,next_date,next_sec,failures,broken
FROMuser_jobs;

JOBNEXT_DATENEXT_SECFAILURESB
--------------------------------------
912501-NOV-9400:00:004N
1414424-OCT-9416:35:350N
4176201-JAN-0000:00:0016Y
3rowsselected.



Youcanalsodisplayinformationaboutjobscurrentlyrunning.Thefollowingsamplequeryliststhesessionidentifier,jobnumber,userwhosubmittedthejob,andthestarttimesforallcurrentlyrunningjobs:

显示当前运行的任务的信息。

SELECTsid,r.job,log_user,r.this_date,r.this_sec
FROMdba_jobs_runningr,dba_jobsj
WHEREr.job=j.job;

SIDJOBLOG_USERTHIS_DATETHIS_SEC
---------------------------------------------------------
1214144JFEE24-OCT-9417:21:24
258536SCOTT24-OCT-9416:45:12
2rowsselected.


转载者新增:
实际开发中可能会遇到要一个月的某个时间定期执行脚本的需求,可以使用
Add_months(to_date(sysdate,''yyyymmdd''),1)来实现,这个脚本就
会一个月执行一次。
例如:
VARIABLEjobnonumber;beginDBMS_JOB.SUBMIT(:jobno,'p_procedure;',to_date(sysdate,'yyyymmdd'),'Add_months(to_date('sysdate',''yyyymmdd''),1)');commit;end;printjobno;


parameter.Forexample,ifyousettheexecutionintervalto'SYSDATE+7'onMonday,butforsomereason(suchasanetworkfailure)thejobisnot
executeduntilThursday,'SYSDATE+7'thenexecuteseveryThursday,notMonday.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐