简体   繁体   English

按月和周间隔安排存储过程

[英]Scheduling stored procedure with month and week intervals

Since Windows Azure doesn't have SQL Agent you'll have to create your own worker role to work like a SQL Agent in order to schedule the running of an stored procedure. 由于Windows Azure没有SQL代理,因此您必须创建自己的辅助角色才能像SQL代理一样工作,以便安排存储过程的运行。

Well I'm all set with this part except of the part with the dates. 好吧,除了带日期的那一部分,我都已经准备好了。 This should be very logic, but it doesn't seems so to me. 这应该是很合逻辑的,但对我而言似乎并非如此。 So how do I check if today is the first day of the month and the first day of the week. 因此,如何检查今天是否是每月的第一天和一周的第一天。

This is what I have so far: 这是我到目前为止的内容:

Trace.WriteLine("SqlWorkerRole entry point called", "Information");

while (true)
{
    DateTime firstDayOfMonth = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
    if (DateTime.UtcNow > firstDayOfMonth)
    {
        Guid? jobId = StartJob("MonthJob");

        if (jobId.HasValue)
        {
            Trace.WriteLine("Working", "Information");
            ExecuteMonthJob();
            StopJob(jobId.Value);
        }
    }
    else
    {
        Thread.Sleep(60000);
    }

    DateTime firstDayOfWeek = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
    if (DateTime.UtcNow > firstDayOfWeek)
    {
        Guid? jobId = StartJob("WeekJob");

        if (jobId.HasValue)
        {
            Trace.WriteLine("Working", "Information");
            ExecuteWeekJob();
            StopJob(jobId.Value);
        }
    }
    else
    {
        Thread.Sleep(60000);
    }
}

I need help with this parts (first day of month) 我需要这部分的帮助(每月的第一天)

DateTime firstDayOfMonth = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfMonth)

and (first day of week) 和(一周的第一天)

DateTime firstDayOfWeek = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfWeek)

Well, you could as an alternative use Enzo Cloud Backup Community Edition (free software). 好吧,您也可以选择使用Enzo Cloud Backup社区版(免费软件)。 It has a scheduler built-in (that requires a x-small worler role) and you can schedule T-SQL calls. 它具有内置的调度程序(需要x-worler角色),您可以调度T-SQL调用。 The scheduler can do weekly, but at this time it doesn't do monthly; 调度程序可以每周执行一次,但目前不每月一次。 still it would be trivial in the stored proc itself to add logic to determine if your proc ran this month or not. 在存储过程本身中添加逻辑来确定您的过程是否在本月运行还是很简单的。

If you do not want to pay for a worker role to do this you can also consider using Cotega which is a service that has the ability to schedule execution of stored procedures . 如果您不想为此付出费用,也可以考虑使用Cotega,它是一项能够安排存储过程执行时间的服务。

Full disclosure, I work on the Cotega service. 全面披露,我从事Cotega服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM