2014-09-01 2 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으로 전화하고 발신자 저장 프로 시저 내부의 날짜를 확인하는 것입니다.

여기 내 발신자 저장 프로 시저입니다. 나는 오라클에 대해 잘 알고 있지만 SQL Server에는 익숙하지 않습니다.

  1. 각 달의 말일에 my_report을 예약하고 시작일과 종료일을 보내는 방법이 있습니까?

코드 아래에있는 내 코드의 괜찮은 버전이 :

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 

T

발견 한 후, 일자리를 만들고 주파수에서

선택 : 오 매월 마지막 날 작업을 실행

가 발생합니다 : 마지막 월간 -

0

당신이 많이 단순화 할 수 있습니다가.

최신 SQL Server를 사용하는 경우 월의 마지막 날을 얻으려면 EOMONTH 함수가 있어야합니다. SQL 서버가 오래된 경우

, 당신은 약간의 트릭을 수행 할 수 있습니다합니다 (MONTH 기능을 사용하여) 월을 현재 DETA (getdate())에 일일를 추가하고 비교 DATEADD 기능을 사용하십시오. 같은 달이라면 그 달의 마지막 날이 아니 었습니다. 그것이 다른 달인 경우, 그것이 그 달의 마지막 날 이었기 때문입니다.

참고 :

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 [email protected], [email protected] where reportenddate is null 
end 
관련 문제