SQL LOOP 将来自 Temp Table 的值作为参数传递给存储过程
SQL LOOP Pass values from Temp Table as parameters to stored procedure
使用 SQL Server 2016,我对我们的财务部门有一个巨大的查询,该查询使用 @Year
和 @FinPeriod
将 t运行sactions 与句点匹配。对于报告,我们通常将单个值传递到存储过程中。但是现在我们需要使用通常会即时生成的数据来填充基础 tables。
有没有人可以帮忙解决的循环?我有一个 temp table,其中包含年份值列和每一年的 finperiod。我希望遍历此 table - 将年和期间都传递给存储过程,一个接一个,直到它们都为 运行。
存储过程元素对我来说很好,只是让 loop/passing 部分工作会有所帮助。
到目前为止我有:
declare @fiscalyearid numeric(9)
declare @FiscalYear numeric(9)
declare @FiscalMonthOfYear numeric(9)
declare @Year numeric(9)
declare @FinPeriod numeric(9)
if object_id('tempdb..#dateloop','u') is not null
drop table #dateloop
select distinct
identity(int,1,1) as ID,
FiscalYear_int, FiscalMonthOfYear
into
#dateloop
from
[DW].[DDS].[dimDate]
where
FiscalYear_int = '2018'
DECLARE C CURSOR LOCAL FAST_FORWARD FOR --
SELECT
ID, FiscalYear_int, FiscalMonthOfYear
FROM
#dateloop;
OPEN C;
FETCH C INTO @FiscalYear, @FiscalMonthOfYear;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC [dbo].[Origen_Reporting->SSRS_Capex_Monitoring_Report_Expenditure] @Year, @FinPeriod
FETCH C INTO @Year,@FinPeriod
END
CLOSE C;
DEALLOCATE C;
任何提示都会很棒。谢谢
我猜您希望您的 Cursor 逻辑起作用。下面是可用于循环遍历日期并在循环中调用 proc 的代码。
DECLARE C CURSOR LOCAL FAST_FORWARD FOR --
SELECT
FiscalYear_int, FiscalMonthOfYear
FROM
#dateloop;
OPEN C;
Fetch next from c into @FiscalYear, @FiscalMonthOfYear
WHILE @@FETCH_STATUS = 0
BEGIN
select @FiscalYear, @FiscalMonthOfYear --exec proc passing these values
EXEC [dbo].[Origen_Reporting->SSRS_Capex_Monitoring_Report_Expenditure] @FiscalYear, @FiscalMonthOfYear
FETCH next from c INTO @FiscalYear,@FiscalMonthOfYear
END
CLOSE C;
DEALLOCATE C;
使用 SQL Server 2016,我对我们的财务部门有一个巨大的查询,该查询使用 @Year
和 @FinPeriod
将 t运行sactions 与句点匹配。对于报告,我们通常将单个值传递到存储过程中。但是现在我们需要使用通常会即时生成的数据来填充基础 tables。
有没有人可以帮忙解决的循环?我有一个 temp table,其中包含年份值列和每一年的 finperiod。我希望遍历此 table - 将年和期间都传递给存储过程,一个接一个,直到它们都为 运行。
存储过程元素对我来说很好,只是让 loop/passing 部分工作会有所帮助。
到目前为止我有:
declare @fiscalyearid numeric(9)
declare @FiscalYear numeric(9)
declare @FiscalMonthOfYear numeric(9)
declare @Year numeric(9)
declare @FinPeriod numeric(9)
if object_id('tempdb..#dateloop','u') is not null
drop table #dateloop
select distinct
identity(int,1,1) as ID,
FiscalYear_int, FiscalMonthOfYear
into
#dateloop
from
[DW].[DDS].[dimDate]
where
FiscalYear_int = '2018'
DECLARE C CURSOR LOCAL FAST_FORWARD FOR --
SELECT
ID, FiscalYear_int, FiscalMonthOfYear
FROM
#dateloop;
OPEN C;
FETCH C INTO @FiscalYear, @FiscalMonthOfYear;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC [dbo].[Origen_Reporting->SSRS_Capex_Monitoring_Report_Expenditure] @Year, @FinPeriod
FETCH C INTO @Year,@FinPeriod
END
CLOSE C;
DEALLOCATE C;
任何提示都会很棒。谢谢
我猜您希望您的 Cursor 逻辑起作用。下面是可用于循环遍历日期并在循环中调用 proc 的代码。
DECLARE C CURSOR LOCAL FAST_FORWARD FOR --
SELECT
FiscalYear_int, FiscalMonthOfYear
FROM
#dateloop;
OPEN C;
Fetch next from c into @FiscalYear, @FiscalMonthOfYear
WHILE @@FETCH_STATUS = 0
BEGIN
select @FiscalYear, @FiscalMonthOfYear --exec proc passing these values
EXEC [dbo].[Origen_Reporting->SSRS_Capex_Monitoring_Report_Expenditure] @FiscalYear, @FiscalMonthOfYear
FETCH next from c INTO @FiscalYear,@FiscalMonthOfYear
END
CLOSE C;
DEALLOCATE C;