How to present job_name to DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION - oracle

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

Related

Procedure to move oracledb export to s3 bucket fails when ran in a job

Job fails with:
ORA-29481: Implicit results cannot be returned to client.
ORA-06512: at "SYS.DBMS_SQL", line 2832
ORA-06512: at "SYS.DBMS_SQL", line 2826
ORA-06512: at "owner.SEND_TO_S3", line 8
ORA-06512: at line 1
I can run the procedure manually(BEGIN SEND_TO_S3; END;) without errors and the db exports
show up in the s3 bucket.
****Oracle Job
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'SEND_EXP_TO_S3_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN SEND_TO_S3; END;',
start_date => SYSTIMESTAMP,
enabled => TRUE,
repeat_interval => 'freq=weekly; byday=mon; byhour=20; byminute=40; bysecond=0;');
END;
****Oracle procedure
(If there some other way than to use a refcursor that might solve this issue as well)
CREATE OR REPLACE PROCEDURE send_to_s3
AS
rc sys_refcursor;
BEGIN
open rc for
SELECT
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'bucket/name',
p_prefix => 'EXP',
p_s3_prefix => '',
p_directory_name => 'DATA_PUMP_DIR') `your text`
AS TASK_ID FROM DUAL;
DBMS_SQL.RETURN_RESULT(rc);
END send_to_s3;
****The oracle error points to an outdated client but I have the latest client and I'm
not sure the scheduler uses a client.
Is there a way to get this job running correctly?

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

oracle dbms_scheduler.create_job() error when executed within stored procedure

Creating job from anonymous block is working fine:
begin
SYS.DBMS_SCHEDULER.create_job(
job_name => 'test_job',
job_type => 'PLSQL_BLOCK',
job_action => 'begin null; end;',
enabled => TRUE,
auto_drop => TRUE);
end;
Result:
PL/SQL procedure successfully completed.
Creating job from stored procedure:
create or replace procedure pr_create_job is
begin
SYS.DBMS_SCHEDULER.create_job(
job_name =>'test_job',
job_type =>'PLSQL_BLOCK',
job_action =>'begin null; end;',
enabled => TRUE,
auto_drop => TRUE);
end pr_create_job;
Begin
pr_create_job;
End;
Result:
Error report - ORA-27486: insufficient privileges ORA-06512: at
"SYS.DBMS_ISCHED", line 135 ORA-06512: at "SYS.DBMS_SCHEDULER", line
271 ORA-06512: at "PR_CREATE_JOB", line 3 ORA-06512: at line 2
27486. 00000 - "insufficient privileges"
As Kaushik Nayak mentioned, CREATE JOB privilege was granted through a role and not directly to the user, granting directly to user solved this error.

Cannot enable job in Oracle Standard Edition using DBMS_SCHEDULER

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.

Associate a scheduler job with a window

Trying to associate a job with a window. Connection should work using the schedule_name but it does not seem to work.
Reproducible example
creating the window opens every other minute
SQL> begin
2 DBMS_SCHEDULER.CREATE_WINDOW (
3 window_name => 'traffic_window',
4 resource_plan => null,
5 repeat_interval => 'FREQ=minutely;interval=2',
6 duration => interval '1' minute
7 );
8 END;
9 /
PL/SQL procedure successfully completed.
SQL> select WINDOW_NAME,SCHEDULE_NAME,SCHEDULE_TYPE,ENABLED,ACTIVE from all_scheduler_windows;
WINDOW_NAME SCHEDULE_NAME SCHEDULE ENABL ACTIV
------------------------------ ----------------------------------- -------- ----- -----
TRAFFIC_WINDOW CALENDAR TRUE TRUE
OK, associating a job
SQL> exec dbms_scheduler.drop_job('test_window_job',true);
PL/SQL procedure successfully completed.
SQL> begin
2 dbms_scheduler.create_job (
3 job_name => 'test_window_job',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'begin test_func(70,''start'',0,null); end;',
6 schedule_name => 'traffic_window',
7 enabled => false,
8 auto_drop => true
9 );
10
11 dbms_scheduler.set_attribute ('test_window_job','max_runs',1);
12 dbms_scheduler.set_attribute ('test_window_job','stop_on_window_close',true);
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> exec dbms_scheduler.enable('test_window_job');
BEGIN dbms_scheduler.enable('test_window_job'); END;
*
ERROR at line 1:
ORA-27481: "HAKI.TEST_WINDOW_JOB" has an invalid schedule
ORA-27476: "HAKI.TRAFFIC_WINDOW" does not exist
ORA-06512: at "SYS.DBMS_ISCHED", line 2751
ORA-06512: at "SYS.DBMS_SCHEDULER", line 1794
ORA-06512: at line 1
Docs for create_job clearly state
schedule_name - The name of the schedule, window, or window group
associated with this job.
Whats the problem ???
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
You are creating a WINDOW but associating the job with the SCHEDULE.
WINDOW != SCHEDULE
Create a SCHEDULE instead of WINDOW.
Update: actually, it is OK to associate a JOB with a WINDOW. But since the windows are in the SYS schema, you must supply the schema name in the parameter:
schedule_name => 'sys.traffic_window'

Resources