PL/SQL procedure successfully completed.
一、sys用户使用dbms_ijob为hou用户创建job
1.查看dbms_ijob.submit的书写格式
sys用户下desc dbms_ijob
PROCEDURE SUBMIT
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
JOB BINARY_INTEGER IN
LUSER VARCHAR2 IN --> Login user when the job was submitted
PUSER VARCHAR2 IN --> User whose default privileges apply to this job
CUSER VARCHAR2 IN --> schema_user
NEXT_DATE DATE IN
INTERVAL VARCHAR2 IN
BROKEN BOOLEAN IN
WHAT VARCHAR2 IN -->procedure
NLSENV VARCHAR2 IN
ENV RAW IN
2.hou用户下创建一个简单存储过程
SQL> conn hou/
Enter password:
Connected.
SQL> create table test(a date);
Table created.
SQL> create or replace procedure pro_insert_date as
begin
insert into test values(sysdate);
end;
/ 2 3 4 5
Procedure created.
3.sys用户为hou创建5号job
SQL> conn / as sysdba
Connected.
SQL> begin
dbms_ijob.submit(5,'HOU','HOU','HOU',
to_date('2012-09-29 12:30:00','YYYY-MM-DD HH24:MI:SS'),'sysdate+1',false,'pro_insert_date;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' NLS_NUMERIC_CHARACTERS=''.,'' NLS_DATE_FORMAT=''yyyy-mm-dd hh24:mi:ss'' NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY''',
'0102000200000000');
commit;
end;
/
匿名块已完成
或者写成
begin
dbms_ijob.submit(5,'HOU','HOU','HOU',
to_date('2012-09-29 12:30:00','YYYY-MM-DD HH24:MI:SS'),'sysdate+1',false,'pro_insert_date;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' '||
'NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' '||
'NLS_NUMERIC_CHARACTERS=''.,'' '||
'NLS_DATE_FORMAT=''YYYY-MM-DD HH24:MI:SS'' '||
'NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY'' ',
'0102000200000000');
commit;
end;
/
注意:1.dbms_ijob.submit创建job时,只能手动指定job号。
2.书写顺序要按照上面的顺序。
3.用户名一定要大写。
注意:上面两条写法是在sql developer中能够执行成功。在sqlplus下无法执行成功,由于涉及到中文货币符号,在这里也就是NLS_CURRENCY=''¥''。
想要在sqlplus下执行成功,将¥换成$即可。至于如何能够在sqlplus下使用中文货币符号,还需要再研究。
4.登录到hou用户查看5号job的情况
SQL> conn hou/
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:42:14 2012-09-30 14:42:14 sysdate+1 N 0
可以看到,sys用户通过dbms_ijob已经为hou用户创建了job。
二、执行5号job
1.在hou用户下
SQL> conn hou
Enter password:
Connected.
SQL> exec dbms_job.run(5);
PL/SQL procedure successfully completed.
2.在sys用户下执行
SQL> conn / as sysdba
Connected.
SQL> exec dbms_ijob.run(5);
PL/SQL procedure successfully completed.
sys通过dbms_ijob.submit为其他用户创建的job,sys和job所在的用户都可以执行。
三、修改job
1.hou用户修改job的执行间隔时间(interval)和下次执行时间(next_day)
SQL> exec dbms_job.interval(5,'sysdate+2');
PL/SQL procedure successfully completed.
查看job情况
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 2012-09-30 14:45:50 sysdate+2 N 0
修改next_day
SQL> exec dbms_job.next_date(5,sysdate+1/24);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
2.sys用户
SQL> exec dbms_ijob.interval(5,'sysdate+2');
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
修改next_day
SQL> exec dbms_ijob.next_date(5,sysdate+5);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
四.中断job
1.hou用户
SQL> exec dbms_job.broken(5,true);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL>
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 4000-01-01 00:00:00 sysdate+2 Y 0
2.sys用户
SQL> exec dbms_ijob.broken(5,true);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> conn hou
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 4000-01-01 00:00:00 sysdate+2 Y 0
五.删除job
1.hou用户
SQL> exec dbms_job.remove(5);
PL/SQL procedure successfully completed.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
no rows selected
2.sys用户
SQL> exec dbms_ijob.remove(5);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> conn hou
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
no rows selected
总结:
1.dbms_job只能在当期用户内创建job、修改和删除job,不能对其他用户的job进行操作;sys用户也无法用dbms_job管理其他用户的job。
2.dbms_ijob只能由sys用户去执行,拥有DBA权限的用户都没有权限去执行它。
3.通过dbms_ijob sys用户可以给其他用户创建job,且job在该用户下,在该用户内可以通过user_jobs视图看到。
4.通过dbms_ijob sys用户能够对其他用户中的job进行删除、修改。
5.sys用户通过dbms_ijob给X用户创建job,那么X用户对该job拥有修改和删除的权限。
dbms_ijob书写格式:
1.创建job
begin
dbms_ijob.submit(jobnu,' LUSER ',' PUSER ',' CUSER ',
next day,'interval',false,'procedure_name;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' '||
'NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' '||
'NLS_NUMERIC_CHARACTERS=''.,'' '||
'NLS_DATE_FORMAT=''YYYY-MM-DD HH24:MI:SS'' '||
'NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY'' ',
'0102000200000000');
commit;
end;
/