合并存储过程的结果集

时间:2016-05-18 02:31:27

标签: sql sql-server stored-procedures

我想结合我的存储过程的结果集。

我正在使用UNION,但它不起作用并抛出这样的错误:

  

Msg 156,15级,状态1,第147行
  关键字“UNION”附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 251
  'END'附近的语法不正确。

我也试过放入临时表但是一旦我开始重用表,它就会说临时表已经存在了。

任何帮助将不胜感激,谢谢。

目前的结果:

lnid       result   Score_result
aaabbbccc  7B       Current

lnid       result   Score_result
aaabbbccc  4D       Override

预期结果:

lnid       result   Score_result
aaabbbccc  7B       Current
aaabbbccc  4D       Override

存储过程:

CREATE PROCEDURE [dbo].[Results]
    --@LanID AS VARCHAR(50) = NULL,
    --@period AS VARCHAR(50) = NULL,
    --@strtdt AS DATETIME,
    --@enddt AS DATETIME
AS
BEGIN
    DECLARE @LanID AS VARCHAR(50)
    DECLARE @period AS VARCHAR(50)
    DECLARE @strtdt AS DATETIME
    DECLARE @enddt AS DATETIME

    SET @LanID = 'aaabbbccc'
    SET @period = 'H1'
    SET @strtdt ='2015-10-01'
    SET @enddt = DATEADD(MONTH, 11, @strtdt )

    IF NOT EXISTS (SELECT * 
                   FROM overridetable
                   WHERE AGENT = @LanID 
                     AND KRA_HALF = @strtdt 
                     AND BIT_CURRENT = 1 
                     AND IS_FULL_YEAR = 0)
    BEGIN
        IF NOT EXISTS (SELECT * 
                       FROM periodictable 
                       WHERE repdate BETWEEN @strtdt AND @enddt
                         AND lnid = @LanID 
                         AND rep_period = @period)
        BEGIN
            IF @period IN ('H1', 'H2')
               (SELECT      
                    lnid, CAST (ROUND(SUM(CAST(numscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)), 0) AS VARCHAR) + ' ' + CAST (CASE WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 1 THEN 'A'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 2 THEN 'B'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 3 THEN 'C' END AS VARCHAR) Result
                                ,'Current' as 'Score_Result'
                 FROM       
                     (SELECT 
                          repdate, lnid, team_Code, numscre, 
                          CASE  
                             WHEN cmpscre = 'A' THEN 1
                             WHEN cmpscre = 'B' THEN 2
                             WHEN cmpscre = 'C' THEN 3 
                          END AS cmpscre,
                          rep_period, 1 AS CounterOfRep,
                          CASE  
                             WHEN rep_period IN ('Q1','Q2') THEN 'H1'
                             WHEN rep_period IN ('Q3','Q4') THEN 'H2' 
                          END AS HalfYearPeriod
                      FROM 
                          periodictable
                      WHERE 
                          rep_period LIKE 'Q%' 
                          AND repdate BETWEEN @strtdt AND @enddt) tmp 
                WHERE       
                    lnid = @LanID 
                    AND halfyearperiod = @period
                GROUP BY    
                    HalfYearPeriod, lnid, team_code)

            ELSE IF @period IN ('FY')
               (SELECT      
                    lnid, CAST (ROUND(SUM(CAST(numscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) AS VARCHAR) + ' ' + CAST (CASE  WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 1 THEN 'A'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 2 THEN 'B'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 3 THEN 'C' END AS VARCHAR) Result            
                                ,'Current' as 'Score_Result'
                FROM        
                    (SELECT 
                         repdate, lnid, team_Code, numscre, 
                         CASE   
                            WHEN cmpscre = 'A' THEN 1
                            WHEN cmpscre = 'B' THEN 2
                            WHEN cmpscre = 'C' THEN 3 
                         END AS cmpscre,
                         rep_period, 1 AS CounterOfRep,
                         'FY' AS FullYearPeriod 
                     FROM 
                         periodictable
                     WHERE  
                         rep_period LIKE 'Q%' 
                         AND repdate BETWEEN @strtdt AND @enddt) tmp 
                WHERE       
                    lnid = @LanID 
                    AND FullYearPeriod = @period
                GROUP BY    
                    FullYearPeriod, lnid, team_code)

                    ELSE

                    (SELECT     lnid, CAST (numscre AS VARCHAR) + ' ' +
                                CAST (cmpscre AS VARCHAR) Result
                                ,'Current' as 'Score_Result'
                    FROM        periodictable
                    WHERE       lnid = @LanID 
                                AND rep_period = @period
                                AND rep_period LIKE 'Q%' 
                                AND repdate BETWEEN @strtdt AND @enddt)

            END

        ELSE

            BEGIN

                (SELECT     lnid, CAST(numscre AS VARCHAR) + ' ' + CAST(cmpscre AS VARCHAR) AS Result 
                            ,'Current' as 'Score_Result'
                FROM        periodictable 
                WHERE       repdate between @strtdt AND @enddt
                            AND lnid = @LanID 
                            AND rep_period = @period)
            END

    END

    ELSE

    BEGIN

        (SELECT Agent AS lnid, CAST(CAST(ROUND(Numerical_Override, 0 ) AS INT) AS VARCHAR) + ' ' + 
                CAST((CASE  WHEN Competency_Override = 1 THEN 'A'
                            WHEN Competency_Override = 2 THEN 'B'
                            WHEN Competency_Override = 3 THEN 'C' ELSE '*' END ) AS VARCHAR) AS Result
                ,'Current' as 'Score_Result'
        FROM    overridetable
        WHERE   AGENT = @LanID AND KRA_HALF = @strtdt AND BIT_CURRENT = 1 AND IS_FULL_YEAR = 0) 


    END

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------






IF EXISTS

    (SELECT * 
    FROM    overridetable
    WHERE   AGENT = @LanID AND KRA_HALF = @strtdt AND BIT_CURRENT = 1 AND IS_FULL_YEAR = 0)




    BEGIN

            IF  NOT EXISTS 

            (SELECT     * 
            FROM        periodictable 
            WHERE       repdate BETWEEN @strtdt AND @enddt
                        AND lnid = @LanID 
                        AND rep_period = @period)


                BEGIN


                    IF          @period IN ('H1', 'H2')

                    (SELECT     lnid, CAST (ROUND(SUM(CAST(numscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) AS VARCHAR) + ' ' +
                                CAST (CASE  WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 1 THEN 'A'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 2 THEN 'B'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 3 THEN 'C' END AS VARCHAR) Result
                                ,'Override' as 'Score_Result'
                    FROM        (SELECT repdate, lnid, team_Code, numscre, 
                                CASE    WHEN cmpscre = 'A' THEN 1
                                        WHEN cmpscre = 'B' THEN 2
                                        WHEN cmpscre = 'C' THEN 3 END AS cmpscre,
                                rep_period, 1 AS CounterOfRep,
                                CASE    WHEN rep_period IN ('Q1','Q2') THEN 'H1'
                                        WHEN rep_period IN ('Q3','Q4') THEN 'H2' END AS HalfYearPeriod
                                FROM periodictable
                                WHERE   rep_period LIKE 'Q%' 
                                        AND repdate BETWEEN @strtdt AND @enddt)tmp 
                    WHERE       lnid = @LanID 
                                AND halfyearperiod = @period
                    GROUP BY    HalfYearPeriod,lnid, team_code)


                    ELSE IF     @period IN ('FY')

                    (SELECT     lnid, CAST (ROUND(SUM(CAST(numscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) AS VARCHAR) + ' ' +
                                CAST (CASE  WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 1 THEN 'A'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 2 THEN 'B'
                                            WHEN ROUND(SUM(CAST(cmpscre AS FLOAT)) / SUM(CAST(counterOfRep AS FLOAT)),0) = 3 THEN 'C' END AS VARCHAR) Result            
                                ,'Override' as 'Score_Result'
                    FROM        (SELECT repdate, lnid, team_Code, numscre, 
                                CASE    WHEN cmpscre = 'A' THEN 1
                                        WHEN cmpscre = 'B' THEN 2
                                        WHEN cmpscre = 'C' THEN 3 END AS cmpscre,
                                rep_period, 1 AS CounterOfRep,'FY' AS FullYearPeriod 
                                FROM periodictable
                                WHERE   rep_period LIKE 'Q%' 
                                        AND repdate BETWEEN @strtdt AND @enddt)tmp 
                    WHERE       lnid = @LanID 
                                AND FullYearPeriod = @period
                    GROUP BY    FullYearPeriod,lnid, team_code)


                    ELSE

                    (SELECT     lnid, CAST (numscre AS VARCHAR) + ' ' +
                                CAST (cmpscre AS VARCHAR) Result
                                ,'Override' as 'Score_Result'
                    FROM        periodictable
                    WHERE       lnid = @LanID 
                                AND rep_period = @period
                                AND rep_period LIKE 'Q%' 
                                AND repdate BETWEEN @strtdt AND @enddt)

            END

        ELSE

            BEGIN

                (SELECT     lnid, CAST(numscre AS VARCHAR) + ' ' + CAST(cmpscre AS VARCHAR) AS Result 
                            ,'Override' as 'Score_Result'
                FROM        periodictable 
                WHERE       repdate between @strtdt AND @enddt
                            AND lnid = @LanID 
                            AND rep_period = @period)
            END


    END

    ELSE

    BEGIN

    (SELECT @LanID AS lnid, '--' AS Result,
            'Override' as 'Score_Result' )

    END

END

1 个答案:

答案 0 :(得分:2)

你需要某处BEGIN

由于原始存储过程工作正常,最简单的方法是创建2个临时表,并在原始select的位置执行select into s。然后在union两个临时表的末尾选择一个。

存储过程的主体看起来像这样:

create temp table 1 ......
create temp table 2 .........

IF NOT EXISTS

(SELECT * 
FROM    overridetable
WHERE   AGENT = @LanID AND KRA_HALF = @strtdt AND BIT_CURRENT = 1 AND IS_FULL_YEAR = 0)

BEGIN
    IF  NOT EXISTS (SELECT     * 
        FROM        periodictable 
        WHERE       repdate BETWEEN @strtdt AND @enddt
                    AND lnid = @LanID 
                    AND rep_period = @period)

        BEGIN

            IF          @period IN ('H1', 'H2')
                select into temp table 1
            ELSE IF     @period IN ('FY')
                select into temp table 1 
            ELSE
                select into temp table 1
        END
    ELSE
        BEGIN
            select into temp table 1
        END
END
ELSE
BEGIN
    select into temp table 1
END

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

IF NOT EXISTS

(SELECT * 
FROM    overridetable
WHERE   AGENT = @LanID AND KRA_HALF = @strtdt AND BIT_CURRENT = 1 AND IS_FULL_YEAR = 0)

BEGIN
    IF  NOT EXISTS (SELECT     * 
        FROM        periodictable 
        WHERE       repdate BETWEEN @strtdt AND @enddt
                    AND lnid = @LanID 
                    AND rep_period = @period)

        BEGIN

            IF          @period IN ('H1', 'H2')
                select into temp table 2
            ELSE IF     @period IN ('FY')
                select into temp table 2 
            ELSE
                select into temp table 2
        END
    ELSE
        BEGIN
            select into temp table 2
        END
END
ELSE
BEGIN
    select into temp table 2
END

(select * from temp table 1) union (select * from temp table 2)