Creating DBMS_SCHEDULER job for oracle - oracle

Trying to create job But can't compile it keeps me given this error. There is a question on oracle forums, it say's that i have to create program to wrap it. Is there any workaround for this?
-- Created on 30.09.2014 by ALI.ORHAN
declare
-- Local variables here
i integer;
begin
-- Test statements here
dbms_scheduler.create_job(job_name => 'blabla'
,job_type => 'STORED_PROCEDURE'
,job_action => 'dingdongprocedure;'
,start_date => '30-OCT-14 10.00.00 PM'
,end_date => '15-JULY-08'
,repeat_interval => 'FREQ=WEEKLY BYDAY=TUE,FRI BYHOUR=10,13'
,enable => 'TRUE'
,comments => 'SUPREME COMMENT');
end;
After i created job from PL/SQL Developer UI, i found out my syntax erorrs, new code is below;
i use sys.dbms_scheduler.create_job instead of dbms_scheduler.create_job. I don't know differances but it's not important alteration.
i used to_date to define start_date, as a fresh-starter i found this better practise.
Important I added job_class parameter to 'DBMS_JOB$'. DBMS_JOB is built_in job class of Oracle RDBMS. So you find all jobs with this query:
select * from ALL_SCHEDULER_JOBS WHERE JOB_CLASS='DBMS_JOB$'
Important My interval's were wrong you should put ; between all parameters like
repeat_interval => freq=weekly;byhour=10, 13
My first job code has another syntax error i use enable instead of enabled.
I set auto_drop false. I guess this parameter is used to drop job when it dones his job. I mean if you create a job that makes changes daily from today to next week. After end-time reaches, this job has dropped. Please correct me if i wrong.
sys.dbms_scheduler.create_job(job_name => 'BOMBASTICJOB'
,job_type => 'STORED_PROCEDURE'
,job_action => 'dingdongprocedure'
,start_date => to_date('30-09-2014 00:00:00'
, 'dd-mm-yyyy hh24:mi:ss')
,end_date => to_date(null)
,job_class => 'DBMS_JOB$'
,repeat_interval => 'Freq=Weekly; ByDay=Tue, Fri; ByHour=10, 13'
,enabled => true
,auto_drop => false
,comments => '');

I am on 12.1.0.1.0. You could create the job in a simple anonymous block :
SQL> BEGIN
2 DBMS_SCHEDULER.DROP_JOB (JOB_NAME => 'test_full_job_definition');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL>
SQL> BEGIN
2 DBMS_SCHEDULER.create_job (
3 job_name => 'test_full_job_definition',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'BEGIN my_job_procedure; END;',
6 start_date => SYSTIMESTAMP,
7 repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
8 end_date => NULL,
9 enabled => TRUE,
10 comments => 'Job defined entirely by the CREATE JOB procedure.');
11 END;
12 /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT JOB_NAME, ENABLED FROM DBA_SCHEDULER_JOBS where job_name ='TEST_FULL_JOB_DEFINITION'
2 /
JOB_NAME ENABL
---------------------------------------- -----
TEST_FULL_JOB_DEFINITION TRUE
SQL>
More examples here

Related

DBMS_SCHEDULER.CREATE_JOB returning "unknown command"

I am trying to create a simple SQL job in Oracle using SQL Developer. This job will run daily and will execute one stored procedure.
The script is:
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"schema1"."jobName"',
job_type => 'STORED_PROCEDURE',
job_action => 'schema1.import1.sp_import',
number_of_arguments => 0,
start_date => TO_TIMESTAMP_TZ('2021-01-12 14:31:21.000000000 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SS.FF TZR'),
repeat_interval => 'FREQ=DAILY;BYTIME=144500;BYDAY=MON,TUE,WED,THU,FRI',
end_date => NULL,
enabled => FALSE,
auto_drop => FALSE,
comments => 'This job will populate date in new table.');
Upon execution of this I am getting below error:
Error starting at line : 2 in command -
DBMS_SCHEDULER.CREATE_JOB (
Error report -
Unknown Command
Any idea?
Cause of that error is, I believe, the fact that you didn't enclose DBMS_SCHEDULER.CREATE_JOB into a BEGIN-END block:
begin
dbms_scheduler.create_job(...);
end;
/
Also, repeat_interval looks wrong (at least, in my 11g). There's no BYTIME - use BYHOUR and BYMINUTE combination instead. Something like this (note that I don't have your procedure):
SQL> BEGIN
2 DBMS_SCHEDULER.CREATE_JOB (
3 job_name => 'test',
4 job_type => 'STORED_PROCEDURE',
5 job_action => 'p_test',
6 number_of_arguments => 0,
7 start_date => TO_TIMESTAMP_TZ('2021-01-12 14:31:21.000000000 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SS.FF TZR'),
8 repeat_interval => 'FREQ=DAILY;BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=14;BYMINUTE=45',
9 end_date => NULL,
10 enabled => FALSE,
11 auto_drop => FALSE,
12 comments => 'This job will populate date in new table.');
13 END;
14 /
PL/SQL procedure successfully completed.
SQL>

Oracle DBMS Scheduler change in frequency on weekends

I have created a oracle dbms scheduler to execute a procedure daily at 05 AM, 10 AM, 03 PM and 08 PM. Below is the scheduler code
DBMS_SCHEDULER.CREATE_JOB
(
job_name => 'TEST_JOB'
,start_date => SYSDATE
,repeat_interval => 'FREQ=DAILY; BYHOUR=05,10,15,20; BYMINUTE=00 ;BYSECOND=0;'
,end_date => NULL
,job_class => 'DEFAULT_JOB_CLASS'
,job_type => 'PLSQL_BLOCK'
,enabled => TRUE
,job_action => 'BEGIN INSERT_IN_TABLE; END;'
,comments => 'TEST JOB'
);
now i have to modify the same scheduler to execute the same procedure only twice on weekends and run at same frequency on weekdays.
I don't want to create a different scheduler for the weekend executions because sometimes the procedure takes more than 5 hours to execute.
Please guide me if there is a better way to achieve this.
One option could be to use embedded calendars, so that you can create your own calendar expression.
Let me show you an example
SQL> BEGIN
dbms_scheduler.create_schedule('my_schedule_c_1', repeat_interval =>
'FREQ=DAILY; BYHOUR=05,10,15,20; BYMINUTE=00; BYSECOND=00; ');
dbms_scheduler.create_schedule('my_schedule_c_2', repeat_interval =>
'FREQ=DAILY; BYDAY=SAT,SUN; BYHOUR=05,10; BYMINUTE=00; BYSECOND=00;');
END;
/ 2 3 4 5 6 7
PL/SQL procedure successfully completed.
SQL> begin
DBMS_SCHEDULER.create_schedule ('MY_CALC', repeat_interval =>'my_schedule_c_1, my_schedule_c_2');
END;
/ 2 3 4
PL/SQL procedure successfully completed.
SQL>
Then , you only need to apply this schedule to your job
SQL> begin
2 DBMS_SCHEDULER.CREATE_JOB
(
job_name => 'TEST_JOB'
,start_date => SYSDATE
3 ,repeat_interval => 'MY_CALC'
4 ,end_date => NULL
,job_class => 'DEFAULT_JOB_CLASS'
,job_type => 'PLSQL_BLOCK'
5 6 7 8 9 10 ,enabled => TRUE
,job_action => 'BEGIN NULL; END;'
,comments => 'TEST JOB'
); 11 12 13
14 end;
15 /
PL/SQL procedure successfully completed.
SQL>
This way, my job will run using the MAIN_CALC schedule, which is a combination of the two different frequencies.
Of course, you can always create two jobs, but in 11g there is no option to create incompatibilities, which is an object in DBMS_SCHEDULER 12c onwards that prevents a job to start until the other is completed.
My advice, use a schedule calendar embedded with multiple frequencies

Oracle scheduler error: Security Group ID (your workspace identity) is invalid

I am trying to create oracle scheduler job (send mail) by pl/sql process (job is created on button click). It create job successfully but job always finish with error:
"ORA-20001: Security Group ID (your workspace identity) is invalid. ORA-06512: at "APEX_050100.WWV_FLOW_SECURITY", line 2939 ORA-06512: at
"APEX_050100.HTMLDB_UTIL", line 3014 ORA-06512: at line 7 ORA-06512:
at line 7.
I also have tried to set security_group_id directly (apex_util.set_security_group_id(p_security_group_id => my_worspace_id or
wwv_flow_api.set_security_group_id(p_security_group_id=>my_worspace_id) but it always finish with the same error as my sample code. When i try to create job manually in sql developer it works. But when job is created by pl/sql process it finish with the mentioned error. Job is created successfully in both cases (pl/sql process or manually) with the same parameters so i do not understand why in case when job is created by pl/sql process it finish with error.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"INVERTORY"."TEST"',
job_type => 'PLSQL_BLOCK',
job_action => 'begin
for c1 in (
select workspace_id
from apex_applications
where application_id = 104 )
loop
apex_util.set_security_group_id(p_security_group_id =>
c1.workspace_id);
end loop;
HTMLDB_MAIL.SEND(
p_to => ''****.****#****.com'',
p_from => ''noreply#****.com'',
p_subj => ''test mail'',
p_body => ''komu'');
end;',
number_of_arguments => 0,
start_date => TO_TIMESTAMP_TZ('2017-08-28 10:29:57.000000000 EUROPE/PRAGUE','YYYY-MM-DD HH24:MI:SS.FF TZR'),
repeat_interval => NULL,
end_date => NULL,
enabled => TRUE,
auto_drop => FALSE,
comments => '');
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => '"INVERTORY"."TEST"',
attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_OFF);
DBMS_SCHEDULER.enable(
name => '"INVERTORY"."TEST"');
END;
Try changing this :
for c1 in (
select workspace_id
from apex_applications
where application_id = 104 )
loop
apex_util.set_security_group_id(p_security_group_id =>
c1.workspace_id);
end loop;
To this :
SELECT MAX(workspace_id)
INTO v_workspace FROM apex_applications
WHERE application_id = 104;
--set workspace - declare v_workspace above as type number
wwv_flow_api.set_security_group_id(v_workspace)
In any event, it would be better to put your logic in a package in the database and create a job with job_type => 'STORED_PROCEDURE' and call on your procedure from there.

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;

What is the syntax for adding parameters to the called procedure in Oracle scheduler?

So say I have 2 procedures: MYPROC1 & MYPROC2(A_PARAM INTEGER)
this works:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'TEST_SCHEDULER',
job_type => 'STORED_PROCEDURE',
job_action => 'developer.MYPROC1', <<<<<<<<<<<<<<<
start_date => TIMESTAMP'2011-12-4 10:30:00',
repeat_interval => 'FREQ=SECONDLY;INTERVAL=30',
end_date => TIMESTAMP'2011-12-4 10:45:00',
auto_drop => FALSE,
comments => 'TEST 1');
END;
replacing line 5 with:
job_action => 'developer.MYPROC1(2)' makes it not work.
Error: ..invalid name for a database object...
So how do I call from a scheduler a parametrized procedure? Whats the syntax?
use job_type => 'PLSQL_BLOCK', job_action => 'BEGIN developer.MYPROC1(2); END;' instead.
Leave your job_action parameter as it is (without the arguments), and add the number_of_arguments option to the number if parameters your procedure expects.
You can then use DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE to set the argument values.
Examples here: Using jobs.
Firsly, create your scheduler job by using DBMS_SCHEDULER.CREATE_JOB without enabling (default is false, already), and then that should be enabled to run.
It' nice to define job_name parameter as a bind variable for consecutive job names.
job_name parameter needn't to be in upper cases, but when querying scheduler run history, call it with all letters of job_name in upper from a privileged schema :
select *
from dba_scheduler_job_log l
where l.job_name = 'TEST_SCHEDULER'
order by l.log_date desc;
Don't forget to include number_of_arguments(for this case it equals 1) parameter for your job creation command.
As Mat tells you may use DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE like in the following example ( also define argument value parameter as a bind variable with value 2 as in your case ) :
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name => '&v_job_name', -- tEst_sCheDuLEr
job_type => 'STORED_PROCEDURE',
job_action => 'developer.MYPROC1',
number_of_arguments => 1,
start_date => '04-apr-2018 10:30:00 am',
repeat_interval => 'FREQ=SECONDLY;INTERVAL=3;',
end_date => '04-apr-2018 10:45:00 am',
auto_drop => false,
comments => 'TEST 1');
DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
job_name => '&v_job_name',
argument_position => 1,
argument_value => &vl -- 2
);
DBMS_SCHEDULER.ENABLE('&v_job_name');
END;
If you get ORA-01882 Timezone region not found error, then
to_timestamp_tz('04-APR-2018 10:30:00 EST', 'DD-MON-YYYY HH24:MI:SS TZR')
or to_date('04.04.2018 10:30:00', 'DD.MM.YYYY HH24:MI:SS')
format may be used for start_date parameter.

Resources