In my application (ASP.NET, C#), i need to run a stored procedure in a set of pre defined time interval(s) everyday. So that i created a sql job and scheduled the same. But the problem is, there is a option to create/modify this time intervals using the application and this will store the modified time intervals in a table. So i need to run the stored procedure in the user configured time intervals.
Now i am doing the following steps to resolve the issue.
- Created a job to execute the stored procedure and scheduled for every 1 min.
- Inside the stored procedure i will check the current time (min) and the scheduled interval(s).
- If it is matched then the tsql code part inside the stored procedure will execute, other wise skip the process.
This is working fine, but the stored procedure will execute every minute (Hope somebody faced the same issue).
Looking for a better solution to solve this issue .
I am not sure how your application or user code works, but you can fire a trigger to the SQL Agent from your user code to start the job by calling https://msdn.microsoft.com/nl-nl/library/ms186757.aspx. The only limitation is that the user needs to be owner of the job or a member of sysadmin, see the link for more details.
1 Create a sql job and create step 1 (to exec your sp) https://msdn.microsoft.com/en-in/library/ms190268.aspx#Anchor_2
2 . Add multiple schedules to the job as per requirement (using sp_add_jobschedule) . details : https://msdn.microsoft.com/en-us/library/ms366342.aspx.
Assuming this isn't a frequent event, execute sp_update_schedule when the table is updated. Add this to the update procedure or as a trigger if directly updating the table.
First thing needed is a little stored procedure for creating interval schedules.
Example usage:
This should create schedule to run every 27 minutes.
You can also need a proc for creating a schedule for specific time:
Example usage:
This should create schedule to run every day at 14:58.
The above two procedures might be easily merged into one. Separated here for clarity and ease of maintenance. They can also be further enhanced, you can parametrize the @freq_type, @freq_interval etc. All you need is in the documentation: https://msdn.microsoft.com/pl-pl/library/ms366342(v=sql.110).aspx
Another step are procedures for updating existing schedules:
And the usage:
You should now be able to create spUpdateSchedule_ExactTime by analogy.
The last thing you need - a stored procedure for deleting schedules:
And its usage:
Or you can easily write alternative which will use schedule_id instead of schedule_name (sp_delete_schedule can get either of those).
NOTICE: In updating and deleting procedures you can use either names or IDs for identifying schedules. While names are more human friendly, and I used them for the examples to be easier to follow, I strongly recommend that you use IDs instead. Names are not forced to be unique, so if you happen to create two schedules with the same name, then both delete and update procs will fail, unless you use schedule_id as a parameter.
It is good that scheduler time is being manage by application.
But in real world, why user will keep updating scheduler time? I mean to say the frequency.
So I think whenever time is modified from application then fire this new stored procedure which will update the scheduler time using
sp_update_schedule
.There is no reason for the stored procedure to execute every minute. It will only fire when scheduler is modified via application.