Cannot enable job in Oracle Standard Edition using DBMS_SCHEDULER - oracle

We're using 11.2 Oracle Standard Edition and cannot enable a job using DBMS_SCHEDULER. Have we struck a bug?
$ sqlplus "/as sysdba"
...
SQL> !cat should.work.sql
begin
dbms_scheduler.create_job(
job_name => 'TEST_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN NULL; END;',
start_date => systimestamp,
enabled => false);
end;
/
select job_name from dba_scheduler_jobs where job_name = 'TEST_JOB';
exec DBMS_SCHEDULER.enable('TEST_JOB');
select job_name from dba_scheduler_jobs where job_name = 'TEST_JOB';
!sleep 2
select job_name from dba_scheduler_jobs where job_name = 'TEST_JOB';
SQL> #should.work.sql
PL/SQL procedure successfully completed.
JOB_NAME
------------------------------
TEST_JOB
PL/SQL procedure successfully completed.
JOB_NAME
------------------------------
TEST_JOB
no rows selected
SQL> show errors
No errors.
I've created an SR, but there's no Metalink articles, so maybe there's an error on my part. Ideas?

Yep. There was a problem in the syntax. This is the answer from Oracle:
The job was created to run only one time. Also if you don't specify
the attribute "auto_drop" to false the job is dropped after the
execution. The default value of auto_drop is true if the attribute is
not specified in create job statement.
Simply add "auto_drop => false" to make the job persistent. Hope this helps anyone else running into this problem.

Related

Insufficient privilege DBMS_SCHEDULER job

I have created a job in Oracle using following scheduler -
BEGIN
DBMS_SCHEDULER.create_job (
job_name => 'MY_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN my_pkg.pull_data(''Y''); END;',
start_date => '31-AUG-21 07.00.00 PM America/New_York',
repeat_interval => 'freq=daily; byminute=0; bysecond=0;',
enabled => TRUE);
END;
/
When I am running job manually using below code -
BEGIN
DBMS_SCHEDULER.RUN_JOB(
JOB_NAME => 'MY_JOB',
USE_CURRENT_SESSION => FALSE);
END;
The job is running as expected. However, scheduled same jobs are failed again and again with below error.
ORA-01031: insufficient privileges ORA-06512: at "SCOTT.MY_PKG", line 246 ORA-06512: at line 1
Any suggestion, what I am doing wrong? I think I have sufficient privileges.
Edit - If I call, individual procedure which is used in the job, it's working as expected. I have checked the line 246, which is a select statement from a table in the same schema as job, proc and other tables used by the proc.
BEGIN my_pkg.pull_data('Y'); END;

How to disable a job in oracle with dbms_scheduler

I created a job which runs in my database successfully with DBMS_SCHEDULER ,but now I need to disable this job, how can i do this?
thanks!
Although the current answers provide a solution to how disable a job, I wanted to go a bit further and explain you how the job is created has an effect on whether the job needs to be disabled in the first place.
I am assuming you are using dbms_scheduler.
Job is created with auto_drop true and enabled true
In this case, once the job is created ( assuming you don't have any start time in the future ) the job executes immediately ( because it is enabled ) and then it is dropped automatically ( auto_drop is true )
SQL> begin
DBMS_SCHEDULER.create_job
(
job_name => 'MY_TEST',
job_type => 'PLSQL_BLOCK',
job_action => 'begin dbms_lock.sleep(5); end;',
enabled => TRUE ,
auto_drop => TRUE
);
end;
/ 2 3 4 5 6 7 8 9 10 11
PL/SQL procedure successfully completed.
SQL>
SQL> exec dbms_lock.sleep(5); -- waiting 5 seconds
PL/SQL procedure successfully completed.
SQL> select job_name,job_action from dba_scheduler_jobs where job_name = 'MY_TEST' ;
no rows selected
SQL>
Job is created with auto_drop to false and enabled to true
In this case, the job runs and it disables itself automatically. In this scenario you don't need to do anything to disable it.
SQL> begin
2 DBMS_SCHEDULER.create_job
3 (
4 job_name => 'MY_TEST',
5 job_type => 'PLSQL_BLOCK',
6 job_action => 'begin dbms_lock.sleep(5); end;',
7 enabled => TRUE ,
8 auto_drop => FALSE
9 );
10* end;
11 /
PL/SQL procedure successfully completed.
select job_name , state, enabled from dba_scheduler_jobs where job_name = 'MY_TEST' ;
JOB_NAME STATE ENABLE
----------------------------------
MY_TEST SUCCEEDED FALSE
Therefore, if your job is enabled is because it has a calendar frequency associated to it, so once has executed, it states enabled until the next time it has to execute
Job with frequency
It means that the job was created to executed based on an expression calendar. In this case, the job executes based on the calendar expression associated to it, and remains enabled and in state SCHEDULED.
SQL> exec dbms_scheduler.drop_job ( job_name => 'MY_TEST' ) ;
PL/SQL procedure successfully completed.
SQL> begin
DBMS_SCHEDULER.create_job
(job_name => 'MY_TEST',
job_type => 'PLSQL_BLOCK',
job_action => 'begin dbms_lock.sleep(5); end;',
enabled => TRUE ,
start_date => systimestamp ,
repeat_interval => 'FREQ=DAILY;BYDAY=MON,TUE,WED,THU,FRI;'
);
end;
/
PL/SQL procedure successfully completed.
SQL> select job_name , state, enabled from dba_scheduler_jobs where job_name = 'MY_TEST' ;
JOB_NAME STATE ENABLE
--------------------------------------
MY_TEST SCHEDULED TRUE
In this case, as it was point out in the other answers:
SQL> exec dbms_scheduler.disable ( 'MY_TEST' ) ;
PL/SQL procedure successfully completed.
SQL> select enabled from dba_scheduler_jobs where job_name = 'MY_TEST' ;
ENABL
-----
FALSE
Summary
If you want to run a job just once and eliminate it, use the option 1 ( auto_drop and enabled )
If you want to run a job and leave it there for run it on demand whenever you want, but disabled. Use option 2 ( auto_drop to false and enabled to true )
Normally you disable jobs that are created with frequency and execute based on some kind of calendar expression.
Obviously, that is just a set of small examples of the many options you have available with dbms_scheduler
Oracle has a good package for schedule jobs.
In your case , you need disable procedure.
Here is the detailed information about dbms_scheduler
Simply call this using oracle new query window like this and your job will be disabled:
begin dbms_scheduler.disable('job-name'); end;
Or in command window :
exec dbms_scheduler.disable('SCHEMA_MNTC_JOB');
You can create a PL/SQL Procedure that disable your job, with the name in parameter
BEGIN
DBMS_SCHEDULER.DISABLE('name_of_your_job');
END;
/
Execute it
EXEC dbms_scheduler.disable('name_of_your_job');
I hope this will solve your problem

DBMS scheduler oracle - how to execute a procedure

I'm loosing my mind. I have a procedure named foo() which takes no arguments. I'd like to execute it, let's say, every 3 minutes. The code I wrote looks like:
BEGIN
dbms_scheduler.create_job(job_name => FooJob,
job_type => 'PLSQL_BLOCK',
job_action => '
BEGIN
foo();
END;',
start_date => SYSTIMESTAMP,
repeat_interval => 'FREQ=MINUTELY;INTERVAL=3;BYHOUR=17;BYMINUTE=35;',
enabled => TRUE
comments => 'A comment.');
END;
/
This gives me an error: identificator 'applyjobpenalities' should be defined.
I based on this example: How to execute a procedure with DBMS_SCHEDULER.CREATE_JOB procedure
Also:
1) How to execute dbms_output.put_line() after execution of foo();? Is it possible to just put this line strightly away?
2) How to check if procedure foo() is (was) executing on behalf of scheduler?
UPDATE:
Ok so what I've done is:
1) I typed in SQL Plus 'set serveroutput on'
2) I made a procedure:
create or replace procedure proc1
IS
BEGIN
dbms_output.put_line('sth');
end;
/
3) I changed scheduler code to:
BEGIN
dbms_scheduler.create_job( job_name => 'JustATest',
job_type => 'PLSQL_BLOCK',
job_action =>
'BEGIN
proc1();
END;',
start_date => systimestamp + interval '10' second,
repeat_interval => 'FREQ=SECONDLY',
enabled => TRUE);
END;
/
But I can't see any result in SQL Plus. What am I missing? Both procedures compiled succesfully and I can see this job when I type:
SELECT * FROM DBA_SCHEDULER_JOBS;
1) There is no way to extract DBMS_OUTPUT from a scheduled job.
2) To check if FOO was executing, I use the following SQL (extracted from TOAD's "Spool SQL to Screen" option. If you are going to be spending any time at all developing in Oracle, get TOAD for Oracle).
SELECT l.job_name
, l.JOB_SUBNAME
, l.log_id "Log ID"
, l.log_date "Log Date"
, l.operation "Operation"
, l.status "Status"
, l.user_name "User Name"
, l.client_id "Client ID"
, l.global_uid "Global UID"
, r.req_start_date "Required Start Date"
, r.actual_start_date "Actual Start Date"
, r.run_duration "Run Duration"
, r.instance_id "Instance ID"
, r.session_id "Session ID"
, r.slave_pid "Slave PID"
, TO_CHAR (r.cpu_used) "CPU Used"
, r.additional_info "Additional Info (Run)"
FROM dba_scheduler_job_log l, dba_scheduler_job_run_details r
WHERE l.log_id = r.log_id(+)
and l.job_name like 'FooJob'
ORDER BY 1 DESC NULLS LAST;
2b) To see jobs that are currently running:
SELECT *
FROM dba_scheduler_running_jobs;
3) If you want to see results from your job, you need to have your job do something, such as insert a record into a table.
Change Job_action => 'proc1';
And in another notepad type
BEGIN
DBMS_SCHEDULER.RUN_JOB(
JOB_NAME => 'justATest',
USE_CURRENT_SESSION => FALSE);
END;
And then execute the above code
You can see result of dbms_output.put_line() in dba_scheduler_job_run_details column "output".
Put in job action
BEGIN
foo();
dbms_output.put_line( 'foo executed in job' ) ;
END;

How to present job_name to DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION

I am having trouble adding email notifications to Oracle 11g (11.2.0.1.0). It seems like a bug but I'm really not sure. I've tried doing this using SQL Developer to build the code as well as examples from the internet but it's not working.
I can create and enable a job easily enough:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"SCHEMA"."test1"',
job_type => 'PLSQL_BLOCK',
job_action => 'begin
null;
end;
',
auto_drop => FALSE
);
DBMS_SCHEDULER.enable(
name => '"SCHEMA"."test1"');
END;
/
anonymous block completed
As a precaution, I remove the job email notification - this works.
BEGIN
DBMS_SCHEDULER.REMOVE_JOB_EMAIL_NOTIFICATION (
job_name => '"SCHEMA"."test1"'
);
end;
/
anonymous block completed
But when I try to add an email notification it's as if it can't find the object, I'm working in my own schema and have the DBA role so I would have thought any potential permission issues should be overcome (though in my own schema I would have assumed I could make scheduled jobs easily enough)
BEGIN
DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION (
job_name => '"SCHEMA"."test1"',
recipients => 'email_address#test.com',
events => 'JOB_BROKEN, JOB_CHAIN_STALLED, JOB_FAILED, JOB_OVER_MAX_DUR, JOB_SCH_LIM_REACHED'
);
END;
/
ORA-27476: "SCHEMA.SCHEMA" does not exist
ORA-06512: at "SYS.DBMS_ISCHED", line 4856
ORA-06512: at "SYS.DBMS_ISCHED", line 7117
ORA-01403: no data found
ORA-06512: at "SYS.DBMS_SCHEDULER", line 4030
ORA-06512: at line 3
Note it says SCHEMA.SCHEMA as if it didn't read the line properly. When I change it from '"SCHEMA"."test1"' to 'test1' it still doesn't work but says ORA-27476: "SCHEMA.TEST1" does not exist.
All my jobs work correctly and behave and I have gotten a UTL_MAIL implementation going but I'd really like to get the oracle stuff working for simplicity if possible.
A possible issue is that by default object names in Oracle are case insensitive, unless you surround them with double quotes.
Here is a test case:
SQL> select * from v$version where rownum = 1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
SQL> create user STACKOVERFLOW identified by STACKOVERFLOW;
User created.
SQL> grant connect, create job to STACKOVERFLOW;
Grant succeeded.
SQL> conn STACKOVERFLOW/STACKOVERFLOW
SQL> l
1 BEGIN
2 DBMS_SCHEDULER.CREATE_JOB (
3 job_name => '"STACKOVERFLOW"."test1"',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'begin
6 null;
7 end;
8 ',
9 auto_drop => FALSE
10 );
11 DBMS_SCHEDULER.enable(
12 name => '"STACKOVERFLOW"."test1"');
13* END;
SQL> /
PL/SQL procedure successfully completed.
If you name your job "test1" Oracle will create it with a lowercase name. You can confirm this by checking the catalog view dba_scheduler_jobs:
SQL> select owner, job_name from dba_scheduler_jobs where job_name = 'TEST1';
no rows selected
SQL> select owner, job_name from dba_scheduler_jobs where job_name = 'test1';
OWNER JOB_NAME
------------------------------ ------------------------------
STACKOVERFLOW test1
Therefore, this will work:
SQL> l
1 BEGIN
2 DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION (
3 job_name => '"STACKOVERFLOW"."test1"',
4 recipients => 'email_address#test.com',
5 events => 'JOB_BROKEN, JOB_CHAIN_STALLED, JOB_FAILED, JOB_OVER_MAX_DUR, JOB_SCH_LIM_REACHED'
6 );
7* END;
SQL> /
PL/SQL procedure successfully completed.
But this won't:
SQL> l
1 BEGIN
2 DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION (
3 job_name => '"STACKOVERFLOW".TEST1',
4 recipients => 'email_address#test.com',
5 events => 'JOB_BROKEN, JOB_CHAIN_STALLED, JOB_FAILED, JOB_OVER_MAX_DUR, JOB_SCH_LIM_REACHED'
6 );
7* END;
SQL> /
BEGIN
*
ERROR at line 1:
ORA-27476: "STACKOVERFLOW.STACKOVERFLOW" does not exist
ORA-06512: at "SYS.DBMS_ISCHED", line 4921
ORA-06512: at "SYS.DBMS_ISCHED", line 7613
ORA-01403: no data found
ORA-06512: at "SYS.DBMS_SCHEDULER", line 4063
ORA-06512: at line 2

DBMS SCHEDULER Job with input

Hi I have a stored procedure in oracle that I would like to run periodically. Firstly I got my DBMS_SCHEDULER Job to compile (see below) and I can even see the job be created and drop it though I don't see the result of the stored procedure occur in the table it is supposed to effect and the stored procedure has been tested.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'JOB_QUERY',
job_type => 'PLSQL_BLOCK', -- see oracle documentation on types --
job_action => 'BEGIN RUNREPORT(''NAME'', ''VERSION'', ''04-Jun-13'', ''11-Jun-13''); END;',
start_date => to_date('2013-08-19 16:35:00', 'YYYY-MM-DD HH24:MI:SS' ),
repeat_interval => 'FREQ=MINUTELY;BYMINUTE=10', -- every 10 minutes.
end_date => NULL,
enabled => TRUE,
comments => 'Daily Jira Query Update');
END;
I was attempting to simply make it run every ten minutes though I see no changes. Also I wanted to be able to pass SYSDATE or the current date to the procedure in the dbms_scheduler job but I cant get it to work with the apostrophes.
Thanks
You have to COMMIT your DML statements. There is no COMMIT in PL/SQL block and I guess in procedure RUNREPORT either.
You don't need an apostrophe around sysdate, it's not a string literal.
job_action => 'BEGIN RUNREPORT(''NAME'', ''VERSION'', sysdate, ''11-Jun-13''); COMMIT; END;',
BYMINUTE does not mean what you would expect. From documentation:
"This specifies the minute on which the job is to run. Valid values are 0 to 59. As an example, 45 means 45 minutes past the chosen hour". What you need is
repeat_interval => 'FREQ=MINUTELY;INTERVAL=10'
You can check next run date and more by querying user_scheduler_jobs.
If you are calling the stored procedure from DMBS Scheduled job you can try below.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
JOB_NAME => 'SCHEMA.MY_DBMS_SCHEDULED_JOB',
JOB_TYPE => 'STORED_PROCEDURE',
JOB_ACTION => 'SCHEMA.STORED_PROCEDURE_TO_BE_CALLED',
START_DATE => '01-AUG-13 12.00.00 AM',
REPEAT_INTERVAL => 'FREQ=DAILY;BYHOUR=0;BYMINUTE=10',
AUTO_DROP => FALSE,
ENABLED => TRUE,
NUMBER_OF_ARGUMENTS => 0,
COMMENTS => 'Scheduled job to perform updates.');
END;
/
To see if your scheduler log you can use below query.
SELECT * FROM all_SCHEDULER_JOB_LOG
where job_name='MY_DBMS_SCHEDULED_JOB'
order by log_id desc;

Resources