循环每个月的存储过程

Loop through stored procedure for each month

以下仅适用于 2020 年 1 月的存储过程

我想 运行 从 'January 2017' , 'February 2017' , 'March 2017'..... 到 2020 年 10 月

每个月
USE [AOR_Archive]
GO

DECLARE @return_value int

EXEC    @return_value = [Report].[Attestation_Overview_SignOff]
        @FinancialPeriod = N'January 2020'

SELECT  'Return Value' = @return_value

GO

存储过程可以这样循环执行:

declare @StartDate as Date = '20170101', @EndDate as Date = '20201001';
-- Tip: To run the SP up to the current month you can use: @EndDate as Date = GetDate();
declare @Date as Date = @StartDate;
declare @FinPeriod as NVarChar(32);
declare @Return_Value as Int;

while ( @Date <= @EndDate )
  begin
  -- Assemble the name of the financial period.
  set @FinPeriod = DateName( Month, @Date ) + ' ' + DateName( Year, @Date );
  -- Execute the stored procedure.
  exec @return_value = [Report].[Attestation_Overview_SignOff] @FinancialPeriod = @FinPeriod;
  -- Display the result.
  select @FinPeriod as FinancialPeriod, @Return_Value as Return_Value;
  -- Advance to the next month.
  set @Date = DateAdd( Month, 1, @Date );
  end;