Schedule a job on specific dates using DBMS_JOB - oracle

I have job, created using DBMS_JOB, which runs daily at 1:00 am
WHAT PROC_XYZ();
NEXT_DATE 16-OCT-13
NEXT_SEC 01:00:00
INTERVAL trunc(sysdate)+1+1/24
I want to change it to run at specific dates (with no pattern or fixed interval), for instance: 30 Oct 2013, 14 Nov 2013, 18 Nov 2013
Is there a way I can specify a list of dates for the job to run?

No.
BUT, you can create a list of the dates that you want the job to run and check whether the current date is one of those dates before executing the procedure. The RETURN statement will return control to the invoking code as soon as it's executed in a procedure.
So, add something like this to the first executed line of your procedure:
if trunc(sysdate) not in (date '2013-10-30', date '2013-11-14') then
return;
end if;
I would probably use a table rather than a list of dates so it's easy to modify without changing code. If you run the job every day then it will exit immediately all days but those you want to it to run completely.
It's worth reading the note at the top of the DBMS_JOB docs:
The DBMS_JOB package has been superseded by the DBMS_SCHEDULER package. In particular, if you are administering jobs to manage system load, you should consider disabling DBMS_JOB by revoking the package execution privilege for users.

Related

PL/SQL That runs on 30th of every month

I am working on a Data Base for a library type project.
I have These tables from which I need data: Books, Reports, Articles.
Using oracle btw.
I need something (trigger or procedure or something else) that runs on the 30th of every month (or the last day), it will then analyze the date of creation of every file in those 3 tables (Books, Reports, Articles) and if the date is older than sysdate - 5 years, then a message should appear with the details of that file (name,date,author).
You have mentioned that message should appear with the details of that file (name,date,author) I am not sure where this should appear.
Still I will give an approach for this problem, you can write a procedure/function in oracle which would write such records(name,date,author) to a log table from which you can see all the details, you can also add a create date, timestamp field to the table (if you want to pull out records based on date). You can use DBMS_SCHEDULER to run the procedure/function every last day of the month. Hope this approach helps.

Create PL/SQL WatchDog/Daemon in Oracle 11gr2

I should create a watchdog that monitor a field in a table every 5 minutes, in Oracle DB. If field has a specific value (a date older than x) an action should be performed.
Is it possible to do it in PL/SQL?
If it's not, I should create a script shell and call it from crontab, or maybe use the Scheduler.
If field has a specific value (a date older than x) an action should be performed.
You could create a TRIGGER. If a new row is inserted such that the date column has a specific value, then you perform some action in the trigger.
Perhaps, you need an AFTER INSERT TRIGGER FOR EACH ROW, since you need to reference the :NEW values.
Here is the link to documentation regarding CREATE TRIGGER.
If you really want to do it as a scheduled job, then you could use DBMS_SCHEDULER. Prior to 10g releases, it was DBMS_JOB.
What you need is to use the DBMS_SCHEDULER package
The DBMS_SCHEDULER package provides a collection of scheduling functions and procedures that are callable from any PL/SQL program. (...)
The Scheduler uses a rich calendaring syntax to enable you to define repeating schedules, such as "every Tuesday and Friday at 4:00 p.m." or "the second Wednesday of every month."
Sorry, maybe I've omitted a detail the field to monitor is for a specific row. I mean if the result of the query:
select up_date from mytab where name_id='test'
is older than 15minutes
then
update mytab set value_col='no' where name_id='test'
But I don't know if it's possible to do it with a trigger. With SCHEDULER I could check at interval time and could be a good work-around.
You may still have a AFTER UPDATE trigger and in that trigger check if the updated row is the one you want to watch. Something like this:
IF :old.name_id = 'test' THEN
-- do something here
END IF;

Oracle Scheduler – Creating a complex schedule with a single job

I have a Job running on an Oracle 10g DB with a pretty simple execution plan.
BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=6,10,14,18
Now the problem is that we have to change the plan – but just for Mondays. So on Mondays the first job should run at 8 instead of 6. Then like all the others (10, 14, 18).
Whereas from Tuesday to Friday it should run as above.
The easiest way would be to create a second job for the Monday and to remove the Monday from the original job.
This, however, is a problem for our application, as it relies on just one job per import.
So the question is: Is there a way to tell the scheduler to run at 6,10,14,18 o'clock TUE-FRI and 8,10,14,18 on MON with a single job?
I read about specifying a repeat interval with a PL/SQL expression. But I didn't find out if there is way to do it.
Thanks for your help
create a composite schedule, and assign that to the job when you create it.
for example:
begin
dbms_scheduler.create_schedule('MY_SCHED1', repeat_interval =>
'FREQ=DAILY;BYDAY=TUE,WED,THU,FRI;BYHOUR=6,10,14,18');
END;
/
begin
dbms_scheduler.create_schedule('MY_SCHED2', repeat_interval =>
'FREQ=DAILY;BYDAY=MON;BYHOUR=8,10,14,18');
END;
/
begin
dbms_scheduler.create_schedule('MAIN', repeat_interval =>
'MY_SCHED1, MY_SCHED2');
END;
/
now you'd just assign the "MAIN" named schedule to the job instead of the schedule string.

PL/SQL cursor problem in procedure

I have stored procedure.In procedure there are three cursors.I have to run procedure daily in production.I want only two cursors should run daily and the remaining cursor should run only on 1st of every month.So what are the changes should be made to third cursor.Please provide the solution.
The cursor itself doesn't actually run. It is your code that uses the cursor. So you can check in code if it is the first day of the month:
-- Check if today is first day of the month
if trunc(sysdate, 'MM') = trunc(sysdate) then
-- Use cursor here
end if;
Possibly a better solution is to create two separate procedures and create jobs for each of them. You can specify intervals for the job so one runs daily while the other runs monthly.
Look into dbms_scheduler

how to execute procedure with timer in oracle

I need to clear all records in a table if the data exist for one hour.
to know the start time, I have a column "StartTime" with "date" data type.
I think I need a timer to do this,
how can I do this in oracle ?
Depending on what your exact requirements are, I would probably look at using a view to only show the valid rows when queried. This would make it seem like only the last one hour of records were available. This would also mean that you don't need to remove rows exactly an hour after they are created.
Then to remove the rows, I would look at using DBMS_JOB or DBMS_SCHEDULER to remove the rows as has been suggested in some of the other answers.
Remember that just because your requirement is to clear the rows from the table after an hour, you probably really only need to remove the ability to query on them, which you could do with a view.
For what version of Oracle?
For version v7.3.4 to 9i, use DBMS_JOB to schedule a task. 10g+, you want to use DBMS_SCHEDULER. It's not clear to me how often you want/need this to run...
You can create scheduled Jobs in Oracle 10G and above using the DBMS_SCHEDULER
If you are really fastidious, you can schedule this job - which calls your procedure - to run every 1 minute so that the data is cleared out as soon as the 60th minute expires.
Refer this link for an example of how to setup / schedule a job via scripts in Oracle 10G
The requirements are not Quite clear.
Is this a job/program that you have to run once and will delete records that existed for more than an hour? If that is the case, you can use..
delete from <table_name>
where StartTime < (sysdate-1/24);
commit;
If you need to purge records constantly, you'll need to schedule this as a job . The frequency will depend on how often you want the records to be deleted.
What is the business case you are trying to solve?

Resources