매월 말에 저장 프로 시저를 실행해야합니다. 현재 달이 끝나고 이전 달의 끝이 시작 날짜로 간주됩니다.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에는 익숙하지 않습니다.
- 각 달의 말일에
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