如何使用 SQL Server sysschedules 模型查询给定日期的所有事件?
给定此模型:sysschedules,并假设数据库中有多个事件具有不同的重复模式组合:每天一次、每三天一次、每周二一次。每 2 周、每月 28 日、每月每 2 个月的第二个星期二、每年 2 月 28 日等等...
给出模型后,我们会立即想到某些字段:
- freq_type 获取事件是否为每日、每周、每月等...
- freq_interval 确定间隔,根据 freq_type
- freq_relative_interval 确定事件是否位于
- freq_recurrence_factor 月份的第 1、第 2、第 3 等,以确定事件发生之间的每周/每月间隔
如果我想查询今天(2012 年 2 月 28 日星期二)的所有事件。 ..那会是什么样子?
更新 这是我到目前为止所想到的(在这个例子中,我想要 2 月 28 日(星期二)发生的所有“事件”: SELECT * FROM TableName WHERE (freq_type = 1 && active_start_date = '2012-02-28') OR (freq_type = 4) OR (freq_type = 8 && freq_interval = 4) OR (freq_type = 32 && 频率间隔 = 3 & freq_relative_interval = 16)
- freq_type 为 1 表示只有一次,在 active_start_date 之前获取
- freq_type 为 4 表示每天,获取所有这些事件,因为它们也发生在这一天
- freq_type 为 8 表示每周,在 freq_interval 4 上获取这些事件(星期二)
- freq_type 为 32 表示每月,相对 - freq_interval 3(星期二),最后的 freq_relative_interval 16
以下是立即跳入脑海的“错误”
1。如果是每周但超过一天(包括星期二)的事件呢?例如,每周二/周四发生的每周事件的 freq_interval 为 20。我将如何重写上面的查询以考虑周二可能的 freq_interval 的所有可能组合?
2.每n周/月发生的事件怎么样?我如何知道这是本周/本月的“开启还是关闭”时间范围?
Given this model: sysschedules, and assuming there were several events in the DB with varying combinations of recurrence patterns: daily, daily every 3 days, weekly on Tues. every 2 weeks, monthly on the 28th, monthly on the second Tues of every 2 months, yearly on Feb 28th, etc...
With the model given, there are certain fields that immediately jump to mind:
- freq_type to get whether the even is daily, weekly, monthly, etc...
- freq_interval to determine the interval, based on the freq_type
- freq_relative_interval to determine if events are on 1st, 2nd, 3rd, etc... of month
- freq_recurrence_factor to determine the weekly/monthly spacing between event occurrences
If I wanted to query for all events that are today, Tuesday 28 Feb, 2012... what would that look like?
Update
Here's what I've come up with so far (in this example I want all "events" that occur on Feb 28 (which is a Tues):SELECT * FROM TableName WHERE (freq_type = 1 && active_start_date = '2012-02-28') OR (freq_type = 4) OR (freq_type = 8 && freq_interval = 4) OR (freq_type = 32 && freq_interval = 3 && freq_relative_interval = 16)
- freq_type of 1 means it's only once, get by the active_start_date
- freq_type of 4 means daily, get all those since they'd occur on this day too
- freq_type of 8 means weekly, get those events on freq_interval 4 (Tues)
- freq_type of 32 means monthly, relative - freq_interval 3 (Tues), freq_relative_interval 16 for last
Here's what immediately jumps to mind as "wrong"
1. What about events that are weekly, but more than one day, including Tuesday? For example, a weekly event which occurs every Tues/Thurs, will have a freq_interval of 20. How would I rewrite the query above to account for all possible combinations of freq_interval which can be Tues?
2. What about events which occur every n weeks/months? How do I know if it's this week/month's "on or off" time-frame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
系统表 sysjobschedules 包含有关作业运行时间和日期/的信息下次运行的时间。您也许可以使用它来查找作业在特定日期何时运行。
这并非没有问题;日期存储为整数,需要进行转换才能使用,并且信息仅在作业步骤完成后更新。因此,您只能返回至少执行过一次的作业的结果。
我从页面的SQL Server代理作业执行信息部分找到了此信息查询 SQL Server 代理作业信息
MSDN 页面底部还有一些社区内容,提供了从整数获取日期时间的 UDF,您可以使用它来解码日期。
更新
在考虑了您的问题并意识到您正在尝试复制
sysschedules
模型而不仅仅是使用它之后,我认为我应该更新我的答案。你问:
我创建了一个包含日期的位掩码值的表,然后将其连接到日程表中
这是周二和周四运行的日程表的结果:
但是,困难的部分是推断未来的日期以获得所有未来的周二和星期四,间隔一个月。这是我留给您的练习:)
我之前创建了一个简化的日程安排系统,您可以使用它来提供几乎同样好的解决方案。您可以模拟在星期二和星期四执行的计划。周四有多个日程条目,以某种方式分组。
我的计划设计包括一个
active_on_day
字段,该字段指示允许运行计划的一周中的哪一天,因此引用多个计划的job
将在这些日子运行。如果您有兴趣,我可以发布表设计和存储过程。The system table sysjobschedules contains information about when a job ran and the date/time when it will next run. You might be able to use that to find when a job will run on a particular day.
It's not without problems; the dates are stored as integers and need converting before they are useful and the information is updated only after the jobstep completes. So you would only get back results for the jobs which have executed at least once.
I found this information from the SQL Server Agent Job Execution Information section of the page Querying SQL Server Agent Job Information
There is also some community content on the bottom of the MSDN page giving a UDF to get a datetime from integer which you can use to decode the dates.
UPDATE
After thinking about your problem, and realising you are trying to replicate the
sysschedules
model and not just use it, I thought I should update my answer.You asked :
I creating a table with the bit mask values for the days and then joined that to the schedule table
Here is the result for a schedule running on Tuesday and Thursday:
However, the difficult part is extrapolating the dates going forward to get all the future Tuesdays and Thursdays with month intervals. That's an exercise I'll leave to you :)
I've previously created a simplified schedule system which you might be able to use to provide a solution almost as good. You can simulate a schedule which executes on Tuesday & Thursday by having multiple schedule entries, grouped in some way.
My schedule design included an
active_on_day
field which indicated the day of the week when the schedule was allowed to run, so ajob
referring to more than one schedule would run on those days. I can post the table design and stored procedures if you are interested.有一个名为 sp_get_schedule_description 的过程,它将以可读格式返回时间表。
所有过程的参数都可以在 sysschedules 表中找到
因此,例如,我创建了一个名为“My Schedule1”的时间表,其中包含以下信息:
如果我运行,
则会给出
如果我将这些值放入 sp_get_schedule_description 中,
以下信息 :可读格式的计划:
问题是它一次需要一个计划,因此您需要想出某种循环来为每个作业调用一次它,将描述放入示例的临时表中,并且您使用参数查询临时表
就像1:
there is a procedure called sp_get_schedule_description that will return the schedule on a readable format.
All the procedure's parameters can be found on the sysschedules table
So, for example, I created a Schedule called "My Schedule1" that has the following information:
if I run
that will give me
if I put these values into sp_get_schedule_description like this:
Ill get the schedule on a readable format:
problem is that it take one schedule at a time, so you would need to come up with some sort of loop to call it once per job putting the description in a temp table ofr example and the you query the temp table with your parameters
like on 1:
无法完全测试。这并没有涵盖所有可能性。例如,如果某个事件每 3 天发生一次,则需要对日期和天数等进行一些计算。
Wasn't able to fully test. This doesn't cover every possibility. For instance, if an event happens every 3 days, some calculating will need to be done for the the dates and days, etc.