Scheduling job in sql server mgmt studio to run for 'x' days in a month - sql

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.

Related

SQL query for inventory management

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.

Dynamic automation scheduling in Toad

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.

Reoccurring SQL job to run on the 2nd and 4th Thursday of every month

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.

Using SQL for an auto scheduler?

I had a question regarding SQL and if you could code it properly to do auto scheduling.
Example
A shipment could come to a store Monday-Friday, but there could only be a certain amount of shipments during this week and it depends which worker was available. I'll run down things in order that way it's not too confusing.
A) A shipment can only stop twice a day, but it cannot stop on a day that has any shipments the day before or the day after. So if Monday had 2 shipments, there would be no stops on Tuesday. If Wednesday has 2 shipments, you could not have any stops Tuesday/Thursday.
B) A shipment can only be so big on these days. If two shipments are both UNDER 1000 pounds, then we can schedule both, but if a shipment is OVER 1000 pounds, then we can only schedule one for that day.
C) One of the two workers must unload each shipment. So lets say that A already has 1 shipment on Monday, I need it to go to B for the 2nd shipment on that Monday.
I understand that SQL cannot accept user input directly, so don't worry about this. Using getdate() would be sufficient enough.
I started on this code and got a little bit in, but the while loop was running for forever (>30 seconds) and I thought it was almost impractical at that point.
Curious if this is doable and the best approach to do this. I'm assuming that C# or something would be loads easier to do with, but wondering about SQL.
Thanks in advance.

T-SQL Task generator script

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).