创建一个迭代另一个存储过程的存储过程?

问题描述 投票:0回答:1

我有一个关于迭代使用存储过程的问题。我创建了一个名为 sp_indicators 的存储过程,它接受一个指标作为参数,并返回一个包含 43437 行和 15 列的表,无论提供什么指标。

我这样称呼它:

EXECUTE z_sch.sp_indicators 'IDS001';

现在,我需要创建另一个名为 sp_indicators_multi 的存储过程,它接受多个指标作为参数并返回结果表的并集。生成的表也应该有 15 列,但总共 43437 * n 行,其中 n 是引入的指标数量。例如,以下行应返回 (173748, 15) 表:

EXECUTE z_sch.sp_indicators_multi 'IDS001, IDS002, IDS015, IDS022';

我的想法是:

  1. 创建临时表
  2. 迭代输入参数
  3. 对于每个指标,执行存储过程
    sp_indicators
    并将结果附加到临时表
  4. 迭代结束后,返回完整的临时表

如何在 SQL Server 中实现此目的?我感谢任何可以帮助我解决这个问题的建议或代码示例。

下面我粘贴了用于构建

sp_indicators
过程的代码:

CREATE PROCEDURE z_sch.sp_indicators
(
    @IDS VARCHAR(50)
)

AS

BEGIN
    DECLARE @sql NVARCHAR(MAX);

    SET @sql = N'
    WITH CTE AS (
        SELECT
            a.[year],
            ' + QUOTENAME(@IDS, '''') + N' AS [indicator],
            a.[gender],
            CASE
                    WHEN a.[age] BETWEEN 0 AND 14 THEN ''00 a 14''
                    WHEN a.[age] BETWEEN 15 AND 39 THEN ''15 a 39''
                    WHEN a.[age] BETWEEN 40 AND 64 THEN ''40 a 64''
                    WHEN a.[age] BETWEEN 65 AND 74 THEN ''65 a 74''
                    WHEN a.[age] >= 75 THEN ''75 o mes'' END AS [age],
            [nse4],
            SUBSTRING([professio_c], 1, 1) AS [prof],
            [nac],
            [irreg],
            [env],
            [ist],
            [nse4c],
            ' + QUOTENAME(@IDS) + N' AS [n],
            1 AS [d],
            AVG(' + QUOTENAME(@IDS) + N' * 1.0) OVER (PARTITION BY a.[year], a.[age], a.[gender]) AS [pond],
            AVG(' + QUOTENAME(@IDS) + N' * 1.0) OVER (PARTITION BY a.[year], a.[age]) AS [pond_s]
        FROM [z_sch].[indicadors_ind] a
        JOIN [z_sch].[abs_ist_entorn] b
        ON a.[abs_c] = b.[codi_abs]
    )
    SELECT
        [year],
        [indicator],
        [nse4],
        [nse4c],
        [ist],
        [env],
        [nac],
        [irreg],
        [prof],
        [gender],
        [age],
        SUM([n]) AS [n],
        SUM([d]) AS [d],
        SUM([pond] * [d]) AS [e],
        SUM([pond_s] * [d]) AS [e_s]
    FROM CTE
    GROUP BY
        [year],
        [indicator],
        [nse4],
        [nse4c],
        [ist],
        [env],
        [nac],
        [irreg],
        [prof],
        [gender],
        [age];';

    -- Execute the dynamic query
    EXEC sp_executesql @sql;
END;```
sql-server stored-procedures azure-synapse temp-tables
1个回答
0
投票
  • 首先需要将参数拆分为多个参数,并使用逗号作为分隔符。为此,您可以使用
    string_split
    功能。
  • 将结果存储在名为 @indicators 的表变量中。
  • 然后使用游标遍历指标并为每个指标执行
    sp_indicators
    存储过程。

下面是

sp_indicators_multi
存储过程的代码。

CREATE PROC z_sch.sp_indicators_multi
(
    @IDS VARCHAR(MAX)
)
AS
BEGIN
    DECLARE @sql NVARCHAR(MAX);


    DECLARE @indicators TABLE (indicator VARCHAR(50));
    INSERT INTO @indicators (indicator)
    SELECT value FROM STRING_SPLIT(@IDS, ',');


    DECLARE @indicator VARCHAR(50);
    DECLARE cur CURSOR FOR SELECT indicator FROM @indicators;
    OPEN cur;
    FETCH NEXT FROM cur INTO @indicator;
    WHILE @@FETCH_STATUS = 0
    BEGIN
        EXECUTE z_sch.sp_indicators @indicator;
        FETCH NEXT FROM cur INTO @indicator;
    END;
    CLOSE cur;
    DEALLOCATE cur;
END;

演示 - 小提琴

© www.soinside.com 2019 - 2024. All rights reserved.