Automating the scheduletable of jobs - sql

There is a table where we had jobnames and there tsrat time stored I want to run a script which will run and check if any jobdidnot start by its scheduletime.
Actually basically I want to create a script which will call my sql query
For example table name is SCHEDULE..column JOBNAME and COLUMN SCHEDULE_TIME
and lets take one example as JObname X should start by 10:30 AM
Thank you ....Please help

Assuming the table that holds what jobs were run and when is called "JOBLOG" and the actual time each job is run is stored in a column called "ACTUAL_TIME" on that table, you would run something like the following:
This will show jobs that started after they were scheduled, or jobs that were scheduled to run 10 or more minutes ago, but which have not yet started (in the latter case, the third column will be null)
select s.jobname, s.schedule_time, l.actual_time
from schedule s
left join joblog l
on s.jobname = l.jobname
where l.actual_time > s.schedule_time
or (l.actual_time is null
and (sysdate - s.schedule_time) * 24 * 60 >= 10)

Related

Automatically execute script for each day

I have a script which calculates the metrics for yesterday automatically and inserts the data into the table, but I want to fill the table with all missing dates.Is it even possible to do automatically or I should manually start the script for each day?
Here is the simplified version of the script:
select sum(amount),id,yesterday
where date < yesterday
group by id
But for example the day before yesterday is also missing in the table, so I want the above script to execute, and also the script:
select sum(amount),id,day_before_yesterday
where date < day_before_yesterday
group by id
use the last date you have in the target table :
select sum(amount),id, max(date)
from table
where date < (select max(date) - interval 1 day from target)
group by id

Automatic update of rows based on sysdate

I have a table TICKET_INFO
It has columns
1.TICKET_ID
2.BOOKING_TIME
3.STATUS
4.ENTRY_TIME
User can book tickets by selecting future time using an app.Suppose when booking, sysdate is 03/10/2020 01.55 PM and user chooses his time as 4.00PM, a record will be created like following.
TICKET_ID| BOOKING_TIME |STATUS | ENTRY_TIME
1 | 03/10/2020 04.00 PM |Pending | 03/10/2020 01.55 PM
What i want is,as soon as sysdate passes 03/10/2020 04.00 PM,the record's STATUS should be updated to Waiting automatically.
100s of records can be created with different timings.
Whats the best practice to achieve this OnTime update. Trigger or any other way?
Realistically, the way to do this is to either create a job that runs periodically and updates the status or to create a view that does the calculation and query that (or a combination of the two). You'd normally create a single job that runs, say, every minute (in which case the status might be set to Pending for a minute past the booking_time). If you really only have the potential to have a couple hundred rows in this table, you could theoretically create a separate job for each row but that approach does not scale well.
The view would be relatively simple
create view vw_ticket_info
as
select ticket_id,
booking_time,
(case when status = `Pending` and booking_time < sysdate
then 'Waiting'
else status
end) status,
entry_time
from ticket_info
If you want to create a job, you'd create a procedure that does the actual update
create or replace procedure update_ticket_status
as
begin
update ticket_info
set status = 'Waiting'
where status = 'Pending'
and booking_time < sysdate;
end;
and then schedule that using the old dbms_job package or, preferrably, the new dbms_scheduler package
exec dbms_scheduler.create_job( job_name => 'Update Ticket Status',
job_type => 'STORED_PROCEDURE',
job_action => 'UPDATE_TICKET_STATUS',
start_date => systimestamp,
repeat_interval => 'FREQ=MINUTELY; INTERVAL=1'
);
You could do both and have the job that periodically updates the actual data in the table and the view that does the calculation at runtime for any rows that the job hasn't updated yet. If you're really concerned about how long the status is still in Waiting, you could set the job to run more than every minute but that can get expensive and there are diminishing returns. If you're processing data asynchronously, you have to accept some delay and if you have to accept some delay, it is pretty rare that a job that runs every minute (thus updating a row, on average, 30 seconds after the booking_time) introduces too much latency.

Database design for task scheduling

I have tasks which need to be repeatedly executed in specified hours and days. For example, task A should be executed at 1, 2 and 3 p.m. on Monday and at 2, 8, 11 p.m. on Friday. Task B should be executed at 4 a.m. on Monday and so on. Basically every task could be executed at any hour of any day. If all tasks for current hour have been executed we execute them all again. So it's like a priority queue for every hour.
I'm using Postgres and right now it's JSONB facilities. I have table tasks which contain task related info and schedule. Schedule for every task looks something like this:
{
"Mo": [
1,
2,
3
],
"Fr": [
4,
5,
6
]
}
But I don't like that json is not strongly typed, query which selects tasks looks really ugly and I think performance of query could be better.
I thought may be I create table where I store each hour of week and, for example, hours and table executions where I store id of tasks correlated to id of hours. Could work, but it seems very bad for storage space(amount of tasks is about 100000 right now and growing).
So I wonder how to design database properly for such case?
Ok, you need to store the tasks, but not a full calendar (create the calendar on the fly when extracting data)...
For each event/task, you will need to store a start datetime and either a duration or an end datetime.
create table TASKS (ID integer constraint TASK_ID primary key,
TASK_NAME varchar(200),
REPEAT_DAY int not null,
REPEAT_TIME time not null);
to select the data, use a CTE to populate a calendar on the fly
with TODAY_CAL as
(
select current_date + (n || ' hour')::INTERVAL as CalTime -- use an appropriate date here, adjust for granularity (hour/minute/second)
from generate_series(0, 23) n
)
select c1.CalTime, t2.TaskName
from TODAY_CAL c1
left join TASKS t2
on datepart('DOW', c1.CalTime) = t2.Repeat_Day
and datepart('Hour', c1.CalTime) = t2.Repeate_Time

Requirement to schedule a job the same date of every month

I have a Teradata script (.sql) provided which has few places of code as 'mmyy', 'yyyymmdd', stamp_dt. The requirement is I want to run this script every month on the day 30th. I need to automate this process in UNIX.
For this, first I need to write few more lines in SQL script to make sure of the date formats. What to do here??
Until now, I've added the below lines at the top of the SQL program.
#! /bin/ksh
# MONTH END DATE, IN FORMAT YYYY-MM-DD
END_DT=$1
ECHO END DATE IS $END_DT
TODAY1=$(date +%Y%m%d%H%M)
But, at few more places I have MMYY. What to do in that case?
And how do I add a month every time? Confused here!!
Part of the code is as follows:
create multiset table udxx.hh000_bank_loan_customer_?mmyy as (
sel acct_num, dept,
case when Total_Amt > 500000
then 'R'
else cus_ind
end as cus_lod
from
hh000_bank_loan_customer a
left join (sel dept_id, type, number from bank_loan) as b
on a.id = b. dept_id
) with data
primary index (ACCT_ID);
collect stats on udxx.hh000_bank_loan_customer_?mmyy index(LOAN_ACCT_ID);
How do i need to modify the above block of code where 'mmyy' present in order for me to schedule later in ksh script of unix every month.
Do i need to give Addmonths in sql program??
I do this sort of thing with unix only
day_of_month=$(date +%d)
if(( day_of_month == 10 ))
then
code goes here
fi
What do you plan to do about February?
Edit starts here
Regarding requirment to replace strings in your script, you can do this sort of thing.
$start_of_month="outside the scope of my answer"
sed "s/start_of_month/$start_of_month/g" original_script > script_for_this_month
script_for_this_month
Cron in unix/linux is what your possibly looking for. Let the system worry about scheduling your script.
http://en.wikipedia.org/wiki/Cron

Publish content exactly 24 hours after it was created

I am using sql server database.it has fields Isactive and created date...say created date=09/11/2010 5.00pm and its intial status=false by 10/11/2010 5.00pm it Isactive should be true.
How to acheive this
Ideally I think you could base the "is record active" info on a date field instead of a bool field
e.g.
-- records that are active 24 hours after their created_date
SELECT AField
FROM SomeTable
WHERE created_date < DATEADD(dd, GETDATE(), -1)
Otherwise, you'll need to have a scheduled job to go through to update the bool flag for all rows that are now active - could need to be run very often if time is given down to full level of accuracy (hh:mm:ss.....) and you need it to become active pretty much straight away.