DBMS Job scheduler for materialized view - oracle

I have been trying to schedule the refresh of three materialized views simultaneously every night. I have used the below code
BEGIN
DBMS_SCHEDULER.CREATE_JOB
(
JOB_NAME => 'REFRESH_MVIEW',
JOB_TYPE => 'PLSQL_BLOCK',
JOB_ACTION => 'BEGIN DBMS_MVIEW.REFRESH("m_view1, m_view2, m_view3",''C''); END;',
NUMBER_OF_ARGUMENTS => 0,
START_DATE => SYSTIMESTAMP,
REPEAT_INTERVAL => 'FREQ=DAILY; BYHOUR=0',
END_DATE => NULL,
ENABLED => TRUE,
AUTO_DROP => FALSE,
COMMENTS => 'JOB TO REFRESH'
);
END;
But I am getting the following error after the job has been run
ORA-12012: error on auto execute of job 57179
ORA-06550: line ORA-06550: line 1, column 495:
PLS-00114: identifier 'm_view1, m_view2' too long
, column :
I understand that there is a constraint of 30 chars in the procedure name. So, does that mean I have to split the job into 3 different jobs? Could you please let me know where I am going wrong?
Thanks in Advance !

Could you please let me know where I am going wrong?
Try to write your statement like that instead:
BEGIN DBMS_MVIEW.REFRESH(''m_view1,m_view2,m_view3'',''C'')
Or
BEGIN DBMS_MVIEW.REFRESH(''m_view1,m_view2,m_view3'',''CCC'')
View list is specified using a string -- so using single quotes. I don't know if spaces are relevant in the view list. In doubt, I've removed them too.
Please note in the first case you have a default refresh for later two views. If you need a complete refresh for all your views, you have to specify CCC as the refresh method.
See https://docs.oracle.com/cd/A97630_01/server.920/a96568/rarmviea.htm#94135

Related

Create oracle scheduler job which runs daily

I want to create oracle scheduler job which runs daily at 20:00 and runs for 30 minute. This job will delete the rows from KPI_LOGS table as this table contains large amount of data and it continues to grow. I have created the below script in oracle sql developer for such job but not sure if this is correct or not as i am new to scheduler job concept.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"RATOR_MONITORING"."CROP_KPI_LOGS"',
job_type => 'PLSQL_BLOCK',
job_action => 'DELETE FROM KPI_LOGS WHERE CAST(TIMESTAMP AS DATE) < (SYSDATE - 28);',
number_of_arguments => 0,
start_date => NULL,
repeat_interval => 'FREQ=DAILY;INTERVAL=30',
end_date => NULL,
enabled => FALSE,
auto_drop => FALSE,
comments => 'CROP_KPI_LOGS');
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => '"RATOR_MONITORING"."CROP_KPI_LOGS"',
attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_OFF);
DBMS_SCHEDULER.enable(
name => '"RATOR_MONITORING"."CROP_KPI_LOGS"');
END;
the repeat_internal is incorrect, what you have there will run every 30 days. to run at 8pm each day go for...
FREQ=DAILY; BYHOUR=20
you can't dictate how long it will run for, it will take as long as your DELETE statement takes

Oracle job scheduler not dropping automatically

I have below oracle job.
dbms_scheduler.create_job
(job_name => m_job_name,
job_type => 'PLSQL_BLOCK',
job_action => 'begin Pkg_Shell.PR_WF_PROC('
|| p_seq_request
|| '); end;',
number_of_arguments => 0,
start_date => sysdate,
repeat_interval => null,
end_date => null,
job_class => 'DEFAULT_JOB_CLASS',
enabled => false,
auto_drop => true,
comments => null
);
The above job is not dropping automatically. This job will run only once. When i went thru various sites it says
For auto drop,This flag if TRUE, causes a job to be automatically dropped after it has completed or has been automatically disabled. A job is considered completed if:
1.Its end date (or the end date of the job schedule) has passed.
2.It has run max_runs number of times. max_runs must be set with SET_ATTRIBUTE.
3.It is not a repeating job and has run once.
My job will run only once. Why my job is not Auto dropped in certain scenarios. ? We couldnt find when it is not dropped. To over come this
If i want to mention end_date like sysdate + 2 hours how to mention it ?
If i want to set max_runs or max_fails how to use that in my job. ? Whether these two settings or anyone above will solve my problem ?
After so long i found the below links that helped me to fix my issue. I have used max_runs set to 1 .
dbms_scheduler.set_attribute(m_job_name,'max_runs',1);
https://community.oracle.com/thread/936850
https://community.oracle.com/message/2458833#2458833

Schedule job that will run as specific user

I'm trying to create a job that will run a stored procedure as a specific user which I can create and provide with the limited permissions to do only what is required.
From what I have read I need to create the user (pretty standard) and then create a credential entry by performing;
dbms_scheduler.create_credential('[cred name]', '[Oracle User]', '[Oracle Password]');
Once I have the credential created I try to use it in the job creation script;
dbms_scheduler.create_job(
job_name => 'myJobName',
job_type => 'stored_procedure',
job_action => 'myStoredProcedure',
credential_name => 'jobRunningUserCredential',
destination_name => NULL);
The problem that I am having is that when this is run I get the following error message;
ora-27351 conflicting values of job attributes credential_name and job_type
I was wondering whether anyone can see something that I've missed out or whether I am just barking up the wrong tree with this approach in which case any recommendations are welcome.
Thanks for any help/advice in advance.
Sam
UPDATE:
After receiving the suggestion to use alter_chain I have implemented the following code;
dbms_scheduler.create_chain('myjob_chain', NULL, NULL, NULL);
dbms_scheduler.define_chain_step('myjob_chain', 'step_name', 'schemaName');
dbms_scheduler.define_chain_step('myjob_chain', 'parallel_instances', 'schemaName');
dbms_scheduler.define_chain_step('myjob_chain', 'enable_job', 'schemaName');
dbms_scheduler.enable('myjob_chain');
dbms_scheduler.alter_chain('myjob_chain', 'step_name', 'credential_name', 'myCredential');
dbms_scheduler.create_job(
job_name => 'myJobName',
job_type => 'stored_procedure',
job_action => 'myStoredProcedure',
credential_name => 'jobRunningUserCredential',
destination_name => NULL);
dbms_output.put_line('job created');
dbms_scheduler.alter_chain('myjob_chain', 'set_parallel_instances', 'credential_name', 'myCredential');
dbms_scheduler.set_attribute(
name => 'myJobName',
attribute => 'parallel_instances',
value => TRUE);
dbms_output_put_line('parallel_instances');
dbms_scheduler.alter_chain('myjob_chain', 'enable_job', 'credential_name', 'myCredential');
dbms_scheduler.enable(name => 'myJobName');
dbms_output_put_line('enable');
The output that this gives me is;
job created
parallel_instances
BEGIN
*
ERROR at line 1:
ORA-27351: conflicting values of job attributes CREDENTIAL_NAME and JOB_TYPE
ORA-06512: at "SYS.DBMS_ISCHED", line 4395
ORA-06512: at "SYS.DBMS_SCHEDULER", line 2803
ORA-06512: at line 67
Any suggestions are greatly appreciated.
IF you want to run a job as a specific user ,
create credential for the particular OS not for the Database (Because the credential is pointing to the OS not to DB)
Create the credential
BEGIN
dbms_scheduler.create_credential(
username => 'OS Username',
password => 'OS password',
credential_name => 'TEST_CREDENTIAL'
);
END;
Create the Job
dbms_scheduler.create_job(
job_name => 'myJobName',
job_type => 'stored_procedure',
job_action => 'myStoredProcedure',
credential_name => 'TEST_CREDENTIAL',
destination_name => NULL);
Now Run the Job ..
What you probably want to do is set the credential for the step.
You can do this using alter_chain on the particular step with
attribute set to credential_name .
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_sched.htm#CHDIAICJ
Credit: Ravi
If you want for the job to be run under userm, say, AJAX - you have to create the job under ths user. For that you have to grant him CREATE JOB privilege.
According to Oracle's documentaion credentials are created as a comntainer for user/password information that is to be passed by Ora scheduler to external programs.
Therefore Your initial script had a misconception problem, trying to create a job with credentials (that is with parameter to call external program) and at the same time with job_type => 'stored_procedure' (and stored_procedure is a job type for calling internal pl/sql procedures).

DBMS_SCHEDULER JOBS Running at morning until night everyday

I want make JOBS use DBMS_SCHEDULER in oracle 10g, where jobs refresh minutely with interval 2 minute running everyday start at 08.00 AM and end at 08.00 PM. I have tried this code,
BEGIN
SYS.DBMS_SCHEDULER.CREATE_JOB
(
job_name => 'UPDATE_REKAP_BALI'
,start_date => trunc(sysdate) + 8/24
,repeat_interval => 'freq=MINUTELY;interval=2'
,end_date => trunc(sysdate) + 20/24
,job_class => 'DEFAULT_JOB_CLASS'
,job_type => 'STORED_PROCEDURE'
,job_action => 'UPDATEREKAPBALI'
,comments => NULL
);
END
but, when i check on the next day, the jobs is not running, i guess that the jobs is never running up again on 08.00 AM at the next day.
Make sure you commit after submitting the job.
Edit This is incorrect: DBMS_SCHEDULER performs an implicit commit, unlike the previous DBMS_JOB which required an explicit commit.
You have to make auto_drop to false , because auto drop will make the job to be dropped once after it is running , so make it as false
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'TEST_J',
job_type => 'CHAIN',
job_action => 'TEST_C',
auto_drop => FALSE,
repeat_interval => 'FREQ=DAILY;BYHOUR=08,09,10,11,12,13,14,15,16,17,18,19;BYMINUTE=02,04,06,08,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,00;BYSECOND=00;',
enabled => TRUE);
END;
/
Auto Drop is enabled by default
http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sched.htm#i1000363
The answer is two links far
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#CIHGCDBJ - Table 93-54 Window Attribute Values
'repeat_interval' line in the table point to "Calendaring Syntax" link http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#BABFBCEF where you can find out how to set a schedule in terms of Oracle

Explain "ORA-01870: the intervals or datetimes are not mutually comparable"

When this code is executed in SQL Developer against Oracle 11g I get an error,
begin
dbms_scheduler.create_job(
job_name => 'comuni_34',
job_type => 'plsql_block',
job_action => 'begin com_auth_api.expire_old_passwords; end;',
start_date => to_date('2009-jan-01 01:15:00', 'yyyy-mon-dd hh24:mi:ss'),
repeat_interval => 'freq=daily',
enabled => true,
comments => 'Expire old passwords'
);
end;
This is the error,
Error starting at line 4 in command:
begin
dbms_scheduler.create_job(
job_name => 'comuni_34',
job_type => 'plsql_block',
job_action => 'begin com_auth_api.expire_old_passwords; end;',
start_date => to_date('2009-jan-01 01:15:00', 'yyyy-mon-dd hh24:mi:ss'),
repeat_interval => 'freq=daily',
enabled => true,
comments => 'Expire old passwords'
);
end;
Error report:
ORA-01870: the intervals or datetimes are not mutually comparable
ORA-06512: at "SYS.DBMS_ISCHED", line 99
ORA-06512: at "SYS.DBMS_SCHEDULER", line 268
ORA-06512: at line 2
01870. 00000 - "the intervals or datetimes are not mutually comparable"
*Cause: The intervals or datetimes are not mutually comparable.
*Action: Specify a pair of intervals or datetimes that are mutually
comparable.
A Google search did not help as it just listed loads of useless Oracle error code sites.
Maybe the source to SYS.DBMS_ISCHED/SYS.DBMS_SCHEDULER can explain this.
Update: A different job that uses '2010-apr-20 01:15:00' instead of '2009-jan-01 01:15:00' just worked maybe the problem is that dates that are too far in the past are not handled correctly.
Update: Using '2009-apr-01 01:15:00' instead of '2009-jan-01 01:15:00' just worked. However '2009-mar-01 01:15:00' did not work so there is limit one how far back a job can be started. Since I have solved my problem I cannot accept an answer that is a repeat of my solution but if someone wants to explain this further I will consider accepting that.
I don't have 11g to test it but on a 10.2.0.4 database the CREATE_JOB was successful with START_DATE as early as 01-JAN-1970. It might be a bug and you may want to check on Metalink if you have access.
I think, you have the wrong set of NLS_LANG* parameters in your session.
SQL Developer does it automaticly. Try this place at the beginnig of the script in sqlplus:
ALTER SESSION SET NLS_LANGUAGE= 'AMERICAN';
ALTER SESSION SET NLS_TERRITORY= 'AMERICA';
So after that try to run:
begin
dbms_scheduler.create_job(
job_name => 'comuni_34',
job_type => 'plsql_block',
job_action => 'begin com_auth_api.expire_old_passwords; end;',
start_date => to_date('2009-jan-01 01:15:00', 'yyyy-mon-dd hh24:mi:ss'),
repeat_interval => 'freq=daily',
enabled => true,
comments => 'Expire old passwords'
);
end;
/
Thanks! This is the only relevant info about that problem I found, Google showed no results...
I faced a similar problem with SQL Developer 18.3.0.277 and Oracle 12c database.
It worked for me several times in the past, but suddenly it was showing that ora-01870 error.
I tried
ALTER SESSION SET NLS_LANGUAGE= 'AMERICAN';
ALTER SESSION SET NLS_TERRITORY= 'AMERICA';
but it didn't help.
What helped at the end was SQL Developer restart, really. Disconnect and connect or drop job/create job was not helping. The error was shown every time when I wanted to enable the job.
I made no alter session or similar before problem occurred. I simply disabled job and when I wanted to enable back again that error was shown.
You might consider the behavior of function OVERLAPS. I don't know if Scheduler uses it, but the error message is the same:
SQL> select 1 from dual where (sysdate,sysdate+2) overlaps (sysdate+1,sysdate+5);
1
----------
1
SQL> select 1 from dual where (null,sysdate+2) overlaps (sysdate+1,sysdate+5);
1
----------
1
SQL> select 1 from dual where (sysdate+2,null) overlaps (sysdate+1,sysdate+5);
1
----------
1
SQL> select 1 from dual where (null,null) overlaps (sysdate+1,sysdate+5);
select 1 from dual where (null,null) overlaps (sysdate+1,sysdate+5)
ORA-01870: the intervals or datetimes are not mutually comparable

Resources