SQL Server:如何在每个日历月结束时运行存储过程

时间:2014-09-01 10:43:44

标签: sql sql-server tsql

我需要在每个日历月末执行存储过程。它应该以当月结束作为结束时间和上个月结束作为开始日期。

这里的例子:

exec my_report @ReportStartDate = '20140731', @ReportEndDate='20140831'
exec my_report @ReportStartDate = '20140131', @ReportEndDate='20131231'
exec my_report @ReportStartDate = '20140228', @ReportEndDate='20140131'

我的目标是将结果存储在表格中。所以我需要创建一个新的存储过程来调用当前的存储过程。

我找不到安排my_report存储过程。所以我创建了一个新的存储过程。我的目标是每天调用caller_sp并检查调用者存储过程中的日期。

这是我的来电者存储过程。我对oracle有很好的了解,但我是SQL Server的新手。

  1. 有没有办法在每个日历月结束时安排my_report并发送开始日期和结束日期。
  2. 我的代码下面有不错的版本
  3. 代码:

    declare @reportstartyear VARCHAR(4) = null
    declare @ReportEndDate DATETIME = null
    declare @ReportStartDate DATETIME = null
    
    if month(getdate()) = '01' 
    Begin
        if DAY(getdate()) = '31' 
        Begin
            set @reportstartyear = year(getdate())-1
            set @ReportStartDate = cast(@reportstartyear + '1231' as Datetime) 
    
            exec [LTR].[LetterOfGuaranteeProceedsReport] 
                 @ReportStartDate, @ReportEndDate = cast(select getdate())
        end
    end
    else if month(getdate())='02' 
    begin
        if year(getdate())%4=0
        begin 
            if day(getdate())='29'
            begin
                set @reportstartyear=year(getdate())
                set @ReportStartDate=cast(@reportstartyear+'0131' as Datetime) 
                exec [LTR].[LetterOfGuaranteeProceedsReport] @ReportStartDate,@ReportEndDate=cast(select getdate())
            end
        end
    end
        else if day(getdate())='28'
        begin
            set @reportstartyear=year(getdate())
            set @ReportStartDate=cast(@reportstartyear+'0131' as Datetime) 
            exec [LTR].[LetterOfGuaranteeProceedsReport] @ReportStartDate,@ReportEndDate=cast(select getdate())
        end
    
    else if month(getdate())='03' 
    begin
        if day(getdate())='31'
        begin 
            if year(getdate())%4=0
            begin
                set @reportstartyear=year(getdate())
                set @ReportStartDate=cast(@reportstartyear+'0229' as Datetime) 
                exec [LTR].[LetterOfGuaranteeProceedsReport] @ReportStartDate,@ReportEndDate=cast(select getdate())
            end
            else 
            begin
                set @reportstartyear=year(getdate())
                set @ReportStartDate=cast(@reportstartyear+'0228' as Datetime) 
                exec [LTR].[LetterOfGuaranteeProceedsReport] @ReportStartDate,@ReportEndDate=cast(select getdate())
            end
    
        end
    end
    

4 个答案:

答案 0 :(得分:2)

你的脚本看起来有点复杂

DECLARE @ReportStartDate date, @ReportEndDate date

-- for sqlserver 2012
SELECT 
  @ReportStartDate = EOmonth(getdate(), -1),
  @ReportEndDate = EOmonth(getdate())

-- for earlier versions
SELECT 
  @ReportStartDate = dateadd(month, datediff(m, 0, getdate()), -1),
  @ReportEndDate =  dateadd(month, datediff(m, -1, getdate()), -1)

EXEC my_report @ReportStartDate, @ReportEndDate

在每个月的最后一天执行工作:

创建一份工作,然后找到并选择

频率不足:

发生:每月 最后一天 - 每1个月

答案 1 :(得分:1)

这是我的剧本。我改成了SP。它工作正常。谢谢你们每个人

declare
@ReportStartDate DATETIME=EOmonth(getdate(), -1),
@ReportEndDate DATETIME=EOmonth(getdate())

if @ReportEndDate=getdate()
Begin

insert into report_log (col1,
col2,
col3
) exec Report @ReportStartDate, @ReportEndDate,@username=null,@LanguageId=null

update report_log set reportstartdate=@ReportStartDate, reportenddate=@ReportEndDate  where reportenddate is null
end 

答案 2 :(得分:0)

你可以简化它。

如果您使用的是现代SQL Server,则可以使用EOMONTH功能来获取该月的最后一天。

如果您的SQL Server较旧,您可以做一些小技巧:使用DATEADD函数将1天添加到当前详细信息(getdate()),并比较月份(使用{{1功能)。如果是同一个月,则不是该月的最后一天。如果它是一个不同的月份,那是因为它是该月的最后一天。

参考文献:

答案 3 :(得分:0)

对于这个问题,我肯定迟到了,我可能会在上下文之外发布答案。但是正如@JotaBe所说(在我的情况下为旧版本),您可以以这种简单方式知道您是否在新月份(如果您有运行第-1个月报告的脚本)。

declare @cDate as date = getDate(), @comDate as Date;
set @comDate = DATEADD(day, -1, @cDate)
if DATEPART(MONTH, @cDate) > DATEPART(month, @comDate)
    print 'Different month'
else
    print 'same month'

如果您可以根据需要进行微调,请进入