DBMS_SCHEDULER.CREATE_JOB returning "unknown command" - oracle

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>

Related

Oracle Job deleted after execution

I'm on oracle 11g
I created a job named "MyJob" to be ran at a fixed date/hour.
The job is well created.
When the job is executed correctly at the fixed date/hour, the job is apparently deleted from the table dba_scheduler_jobs, I verified with this query:
SELECT owner, job_name, enabled FROM dba_scheduler_jobs;
My question is why the job is "deleted" ?
My job creation script:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'MyJobTest',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN insert into test values (''01'',sysdate); END;',
start_date => '01/12/16 17:00:00,000000000 +01:00',
repeat_interval => NULL,
end_date => NULL,
enabled => TRUE,
comments => ' Insert into Test Table');
END;
/
Thanks in advance.
If you don't specify a repeat_interval and the auto_drop parameter is set to true (the default if you don't specify the parameter at all), Oracle will drop the job once it is run. Generally, if you're creating a job that runs once, there is no point in leaving it around forever.
You can tell Oracle not to auto drop the job by specifying an auto_drop value of false.
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'MyJobTest',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN insert into test values (''01'',sysdate); END;',
start_date => '01/12/16 17:00:00,000000000 +01:00',
repeat_interval => NULL,
end_date => NULL,
enabled => TRUE,
auto_drop => false,
comments => ' Insert into Test Table');

Creating DBMS_SCHEDULER job for 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

Oracle Job Scheduler doesn't repeat the job_action

I want to repeate a Job every 5 minutes. I have a test table that I fill with random dates. If there are dates older then SYSDATE-5, than I want to delete them. The following code works only the first time I start the scheduler and it never repeats the job_action agaian:
BEGIN
SYS.DBMS_SCHEDULER.CREATE_JOB (
job_name => '"AUTHMGR"."Test2"',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN DELETE FROM TEST WHERE TESTDATE < SYSDATE-5;END;',
number_of_arguments => 0,
start_date => SYSDATE,
repeat_interval => 'FREQ=MINUTELY;INTERVAL=5',
end_date => NULL,
job_class => '"SYS"."DEFAULT_JOB_CLASS"',
enabled => TRUE,
auto_drop => FALSE,
comments => 'Test');
END;
/
Do I use the repeat_interval with wrong FREQ and wrong INTERVAL?
I use the Scheduler in Oracle SQL Developer.
The problem was with the INSERT statements. There was no COMMIT after INSERT.

job scheduling for a few hours everyday

I need to schedule a job starting from 0600 till 1800. The job should run after every two hours. For example 0800, 1000, 1200, 1400, 1600, 1800.
Here is the code I have managed to do so far:
DECLARE
l_id binary_integer;
begin
sys.dbms_job.submit(job => l_id, what => 'integration_export;', interval => 'TRUNC(SYSDATE,''hh24'')+0/24/60');
sys.dbms_output.put_line(l_id);
end;
This will, of course, run the job after every 2 hours without stopping at 1801 - 0759. How may I add this restriction?
One thing I though is to create another schedule procedure which wakes up at 1801 and changes NEXT_DATE for this job. However, I am wondering if it is a good idea.
Any suggestions?
Thanks in advance :-)
dbms_job is old. I'd recomend you use the dbms_scheduler (introduced in Oracle 10g) instead.
dbms_scheduler.create_job(job_name => 'YOUR_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'integration_export;',
start_date => systimestamp,
repeat_interval => 'freq=hourly; byhour=8,10,12,14,16,18; byminute=0; bysecond=0;',
enabled => true,
auto_drop => false,
comments => 'some comment about the job');
Instead of dmbs_job, use the advanced dbms_scheduler. Here is an example:
begin
DBMS_SCHEDULER.create_job (
job_name => 'Integration_export',
job_type => 'PLSQL_BLOCK',
job_action => 'integration_export;',
start_date => SYSTIMESTAMP,
enabled => TRUE,
repeat_interval => 'freq=daily; byhour=6,8,10,12,14,16,18; byminute=0; bysecond=0');
end;
/

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