I have been following Stackoverflow for years but posting for the first time. I am trying to dynamically schedule a script in Toad but there doesn't seem to be an option for it. For example, I want to automatically trigger a script on lets say 2nd Feb, 2020. Then it should run after 4 weeks on 1st March 2020. Then after 5 weeks on 5th April, 2020. Then after 4 weeks on 3rd May, 2020 and so on. Is there a way of achieving this in Toad automation?
TIA
I use TOAD as a GUI to access Oracle database(s). I don't know which database you use. Nonetheless, from my point of view, you should schedule a database job.
In Oracle, you'd use DBMS_JOB or DBMS_SCHEDULER. The latter is capable of running even an operating system script (if that's a "script" you are talking about). Otherwise, usually we schedule a stored procedure (which resides in the database).
If, on the other hand, you'd want to run that "script" on the operating system level only, then check your operating system scheduling capabilities. On MS Windows, that would be the Task Scheduler; on UNIX, it is cron.
As intervals you'd want to run that script isn't straightforward (e.g. "on 2nd of every month" or "every 5 weeks" and similar) but "today, 4 weeks later, 5 weeks later, 4 weeks later, 5 weeks later, ...", you'll probably have to develop some logic to do so. For example, create a calendar table and mark days on which you want to run that script, schedule it to run daily but check whether today's data equals the one which is marked. Or, create two jobs: one that starts "today" and runs every 10 weeks, and the one that starts "today + 4 weeks" and runs every 10 weeks.
Related
Hope I can explain the problem I'm having trouble with.
I have to write a stepwise methodology using pseudocode/SQL query to auto generate a list of products/items with low stock/expiry from the inventory database.The list must be updated at 12 a.m. daily.
I tried this
CREATE EVENT IF NOT EXISTS update_table
ON SCHEDULE EVERY 1 DAY STARTS '2022-05-22 00:00:00'
ON COMPLETION PRESERVE ENABLE
Do
Select inventory.products from inventory where inventory.stocks <
inventory.required_stocks.
Your stated requirement is to run some sort of report very soon after the beginning of each calendar day.
The next question you must answer is this: What will you do with that report? Will you simply drop it into "low_stock" table someplace in your database? Will you format it into an email message and send it to your purchasing department? It will be difficult to make "pseudocode" for your requirement without first analyzing the overall business process you intend to enhance.
Various RDBMS systems have ways of doing scheduled things at particular times of day. You've shown the EVENT setup provided by MariaDB / MySQL. SQL Server has their "Jobs" system. postgreSQL has the pg_cron extension. Yo
The thing is, you can't just do SELECT operations from within these scheduled database actions: the result sets have noplace to go from that context. You can do CREATE TABLE midnight_run AS SELECT whatever ... to place the results in a table. But then the results are in another table.
If you want to get the results out of the DBMS, you'll need a UNIXish cron job or a Windowsish scheduled task running an appropriate application at midnight each day.
Pro tip Do your best to avoid scheduling stuff for precisely midnight. Many things run then. If you wait until a couple of minutes after the hour, your code is less likely to contend with other midnight code.
I need a job to run on the 2nd and 4th Thursday of every month. I know I can set up a job to run every two weeks but that logic doesn't work when a month eventually hits a five week span. Is it possible to set this up using the SQL Server Agent Schedule? If not, does anyone recommend another method?
Create two schedules on the job, one that runs on the second Thursday, and one that runs on the fourth.
Under the Frequency heading on the Scheduler, change the Occurs drop down to Monthly to get to the options you need.
I am fairly new to SQL databases and stored procedures so I apologize in advance if this question is too general or dumb... I have made a web form using ASP.NET Core 2.2 MVC and it is connected to an Azure SQL database. The form takes in 3 inputs: annual cost [decimal (19,4)], start month [date], duration [int].
For example, if the user enters 120,000 for the annual cost, 09/01/2019 for start month, and 12 months for duration, then I would like the script to cut the annual cost by the duration and populate a table called FY19 with 10,000 for the column September, 20,000 for the column October, 30,000 for the column November, 40,000 for the column December, then go to another table named FY20 and continue filling it in until 120,000 for the column August.
My question is, should I write this script as a stored procedure? Or write it as a method in my project controller? If I write is as a stored procedure, is it possible to automate it so that every time new data gets inserted into the main table, the script fills in the FYxx tables?
Again, I apologize for making such a general question but I am kinda stuck in this problem. Any guidance would be appreciated and if anyone is willing to take an hour of their day to help me through skype I would greatly appreciate it.
You can use TRIGGERS to automatically invoke SQL script.
You can do it manually in the controller too but I think it is more convenient to do with TRIGGERS.
You can computed column on your 2nd table using the ID of your main table. Here's the link on how to add computed columns.
https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-2017
I need to create a T-SQL (I`m using SQL Server 2008 Express) script (procedure/function) that takes entries from a "Tasks" table and generates only the tasks planned to occur in the next 5 hours.
In the "Tasks" table I should have tasks with job like properties :
Occurrence : One time or Recursive
Frequency : every x Days, Weeks, Months
Depending on the Frequency :
For Daily : every x hours,minutes
For Weekly -Days of the week when the task should occur
For Monthly - The first,second,last -day,week day,weekend -
day of week OR The 1st, 2nd, 3rd..31st of the month.
When I run the script I should get only the tasks that will occur in the next 5 hours or less in the case if task occurs every x minutes.
So what this script should actually do is use all the options from a SQL Server Job for a task.
Example1: I create a task "Check email" that occurs every day, between 14:00 and 15:00 . When i run the script at 09:00 I would get Task:"Check email" Time:5 hours left until task, because it occurs every day, i should be able to get this result every day when I roll the script ,only if that 5 hours or less range .
Example2: A task "Send hours report" that occurs Monthly, on the 1st week day (not weekend), at 01:00 PM . Running the script on the first week day of the month at 08:00 AM I should get Task:"Send hours report" Time:5 hours until task
I know it's a quite big request but hopefully someone will find it easy what for me seems pretty hard.
You should also store somewhere the date/time of the last execution of each job (at least for jobs that are configured for every x hours/minutes). The idea is to calculate the next execution date/time for each job, and return it if it's less than 5 hours away. If the job is configured for every x hours/minutes, use DATEADD and check if the result is within the configured timeframe. If the job is configured for daily (at a specified time), just check if the specified time is less than 5 hours away (but still in the future). I have done a similar feature and it's not an easy thing to check for all cases (it took me a few days of coding).
How do I run a job in Sql Server Agent in (SQL Server Management Studio) for 'x' number of days in a month?
eg: Every 1st.2nd and 3rd of every month
The problem is I am not sure of these days! The user will specify the 'x' number of days!
For instance, the user feels for the month of April he wants the job to be scheduled on 'x' days. Here x= 1st,2nd and 4th of April.
And for the month May he feels to schedule on 'x' days. Here x= 7th,8th and 10th!
Is it possible to schedule in such a way?
Is there any script to schedule this???
Make a schedule that's run on those three days. Assign this job to that schedule.
(To elaborate and make it perfectly clear).
Make three schedules (Rightclick Jobs, Manage Schedules).In new schedules choose - Recurring on monthly. One starts the 1st, one starts the 2nd, one starts the 3rd of every month (for example, or x times for whatever day of month you wish).
Name these schedules something that's easy for you to spot in a list.
On the job you wish to run on these days, properties on it, schedule, then pick the schedule(s) you want to assign to the given job.
That should be all.
If the schedule is user defined then I'd probably just schedule the job to run every day and have the first step be to check whether it should be running today and if not exit.
Check Howto Schedule a Job and follow the Instructions of cairnz.