2014-09-01 42 views
1

我需要在每個日曆月末執行存儲過程。它應該以本月的結束和上個月的結束作爲開始日期。SQL Server:如何在每個日曆月末運行存儲過程

下面的例子:

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. 有我的代碼如下

代碼像樣的版本:

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 

回答

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個月

0

可以簡化了很多。

如果您使用的是現代SQL Server,則需要EOMONTH函數才能獲取該月份的最後一天。

如果您的SQL Server是年紀大了,你可以做一個小竅門:用DATEADD功能爲1天添加到當前DETA(getdate()),並比較月份(使用MONTH功能)。如果是同一個月,那不是該月的最後一天。如果這是不同的月份,那是因爲這是本月的最後一天。

參考文獻:

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 [email protected], [email protected] where reportenddate is null 
end 
相關問題