存储的过程会在SQL Server中提供多个结果集而不是一个结果集

时间:2018-09-18 07:37:49

标签: sql-server tsql ssms

我正在使用SQL Server,并使用SQL Server 2014 Management Studio进行查询。

我正在一个数据库连接中调用一个存储过程,该存储过程给出了多个结果集。

通过“结果集”,我的意思是它给出了多个输出,例如如果我们查询两个select语句,我们将如何获得两个结果集。输出中的行数无关紧要。

这是一个问题,因为如果我在另一个数据库连接中查询它,它只会给出一个预期的结果集。我预计存储过程的任何数据库设置都会出现问题。

P.S。我的过程使用UNION ALL,将所有内容存储在临时表中,然后提供此临时表的输出。

这是我的程序:

CREATE PROCEDURE [dbo].[PROC] 
(
    @effDate DATE,
    @EffWeekDate DATE
) WITH recompile 
AS 
BEGIN 

    SET TRANSACTION isolation level READ uncommitted 

    EXEC pos..sp_nms_check_frag 
      @table_name = 'abc', 
      @execute_command = 1, 
      @include_investigation = 0, 
      @include_indexes = 0, 
      @include_statistics = 1; 

    EXEC pos..sp_nms_check_frag 
      @table_name = 'def', 
      @execute_command = 1, 
      @include_investigation = 0, 
      @include_indexes = 0, 
      @include_statistics = 1; 

    EXEC los..sp_nms_check_frag 
      @table_name = 'xyz', 
      @execute_command = 1, 
      @include_investigation = 0, 
      @include_indexes = 0, 
      @include_statistics = 1; 

    EXEC los..sp_nms_check_frag 
      @table_name = 'pqr', 
      @execute_command = 1, 
      @include_investigation = 0, 
      @include_indexes = 0, 
      @include_statistics = 1; ; 

    WITH tmp_hierachy 
    ( 
        column1, 
        column2, 
        column2_parent, 
        column3, 
        column4, 
        column5, 
        column6 
    ) 
    AS 
    ( 
        SELECT column2 'column1', 
        column2, 
        column2_parent, 
        a.column3, 
        a.column4, 
        a.column4 AS column5, 
        a.column6 
        FROM pos..abc A 
        WHERE a.rds_eff_dt = @effDate 
        AND a.column2 IN 
        ( 

            SELECT column2 
            FROM   los..xyz 
            WHERE  @EffWeekDate BETWEEN rds_start_dt AND    rds_end_dt 

            UNION ALL 

            SELECT column2 
            FROM   los..pqr 
            WHERE  @effDate BETWEEN rds_start_dt AND    rds_end_dt
        ) 
        AND    a.column2 != a.column2_parent 

        UNION ALL 

        SELECT tmp.column1, 
        a.column2, 
        a.column2_parent, 
        a.column3, 
        a.column4, 
        tmp.column5 - 1, 
        a.column6 
        FROM       pos..abc A 
        INNER JOIN tmp_hierachy tmp 
        ON         a.rds_eff_dt = @effDate 
        AND        a.column3 = tmp.column3 
        AND        tmp.column2_parent = a.column2 
        AND        tmp.column5 >= 0 

    ) 

    SELECT * 
    INTO   #tmp_hierachy 
    FROM   tmp_hierachy 

    SELECT a.column3, 
    a.column1, 
    a.column2, 
    column2_parent, 
    column4, 
    column5 AS column5, 
    column6, 
    b.column7, 
    b.column8, 
    b.column9 
    FROM       #tmp_hierachy A 
    INNER JOIN pos..def B 
    ON         a.column2 = b.column2 
    AND        b.rds_eff_dt = @effDate 
    AND        EXISTS 
    ( 
        SELECT 1 
        FROM   
        ( 
            SELECT column2, 
            column3, 
            Max(column5) AS max_level 
            FROM #tmp_hierachy 
            GROUP BY column2, 
            column3
        ) AS grp_node 
        WHERE  grp_node.column2 = a.column2 
        AND    grp_node.column3 = a.column3 
        AND    grp_node.max_level = column5
    ) 
    ORDER BY column1, 
    column3, 
    column4, 
    column2_parent, 
    column2 OPTION (recompile)

END

0 个答案:

没有答案