将Case语句嵌套在IF语句中

时间:2012-05-09 16:56:08

标签: sql db2 case

我在where子句中的最后一个CASE语句上遇到编译器错误。我似乎无法弄清楚我做错了什么......任何帮助都将不胜感激,谢谢。

P1: BEGIN
DECLARE cursor1 CURSOR WITH RETURN FOR
    SELECT DISTINCT MPWBIPS.MIP_NO,                                  
        MPWBIPS.ITEM_NO,                                 
        MPWBIPS.MATL_PLNR_ID,                            
        MPWBIPS.ITEM_OBS_STATUS,                       
        MPWBMIT.EXCS_ITEM_COST_TOT,
        MPWBMIP.TRGT_CHGOVR_DATE,
        SLOTLOC.LOT_FROM_EFF_DATE,              
    FROM MPWBIPS MPWBIPS                                          
    INNER JOIN MPWBMIT MPWBMIT 
        ON MPWBMIT.MIP_NO = MPWBIPS.MIP_NO             
        AND MPWBMIT.ITEM_NO = MPWBIPS.ITEM_NO 
    INNER JOIN MPPLNR MPPLNR
        ON MPWBIPS.MATL_PLNR_ID = MPPLNR.MATL_PLNR_ID
    INNER JOIN MPWBMIP MPWBMIP
        ON MPWBIPS.MIP_NO = MPWBMIP.MIP_NO
    INNER JOIN SMPROJ_DPIMS SMPROJ
        ON MPWBIPS.MIP_NO = SMPROJ.MIP_NO
    INNER JOIN SLOTLOC_DPIMS SLOTLOC
        ON SMPROJ.MFG_CHGOVR_PER = SLOTLOC.LOT

    WHERE plannerID =
        CASE WHEN mgrIndc = 'Y' THEN
            MPPLNR.MGR_LID
        WHEN suprvIndc = 'Y' THEN
            MPPLNR.SPRVSR_LID
        WHEN plannerID = '' THEN
            ''
        ELSE
            MPPLNR.MATL_PLNR_ID
        END

        AND CASE WHEN obsStatus = 'ACTION' THEN
                    MPWBIPS.ITEM_OBS_STATUS <> 'RESOLVED 99'
                    AND MPWBIPS.ITEM_OBS_STATUS <> 'EXCESS 90'
                WHEN obsStatus = 'ALL' OR obsStatus = '' THEN
                    obsStatus = ''
                ELSE
                    MPWBIPS.ITEM_OBS_STATUS = obsStatus
                END
        AND (COALESCE(mipNo, '') = '' OR MPWBIPS.MIP_NO = mipNo);
OPEN cursor1;

结束P1

2 个答案:

答案 0 :(得分:2)

不要以为你可以在这种方法中使用if。但是你可以嵌套案例。

case when parm1 <> '' THEN
            CASE parm1 
                WHEN '1' THEN
                    --do something
                WHEN '2' THEN
                    --do something
                ELSE
                    --do something
            END
end

但为什么呢?而只是添加到您的案例

            CASE parm1

                WHEN '1' THEN
                    --do something
                WHEN '2' THEN
                    --do something
                WHEN <> ''
                    --do something
            END

答案 1 :(得分:1)

您无法在case语句中嵌套if语句。考虑一下,重写你的查询。

你最后遗漏了case。你的案例陈述应该是

CASE WHEN obsStatus = 'ACTION' THEN 
MPWBIPS.ITEM_OBS_STATUS <> 'RESOLVED 99'
AND MPWBIPS.ITEM_OBS_STATUS <> 'EXCESS 90'
WHEN obsStatus = 'ALL' OR obsStatus = '' THEN                     
obsStatus = ''                 
ELSE                     
MPWBIPS.ITEM_OBS_STATUS = obsStatus
END CASE 

编辑:

<>(不等于)运算符正在创建问题;你不能在那里使用它们。不确定你使用的是不相等的情况。低于两行

MPWBIPS.ITEM_OBS_STATUS <> 'RESOLVED 99'
AND MPWBIPS.ITEM_OBS_STATUS <> 'EXCESS 90' 

可能会使你的Where条件像这样:

WHERE plannerID = 
(CASE WHEN mgrIndc = 'Y' THEN             
MPPLNR.MGR_LID         
WHEN suprvIndc = 'Y' THEN             
MPPLNR.SPRVSR_LID         
WHEN plannerID = '' THEN  ''         
ELSE MPPLNR.MATL_PLNR_ID         
END)          

AND 

((obsStatus = 'ACTION') and (MPWBIPS.ITEM_OBS_STATUS <> 'RESOLVED 99'
 AND MPWBIPS.ITEM_OBS_STATUS <> 'EXCESS 90'))

or

((obsStatus = 'ALL' OR obsStatus = '') and (obsStatus = ''))

or

MPWBIPS.ITEM_OBS_STATUS = obsStatus

AND 

(COALESCE(mipNo, '') = '' OR MPWBIPS.MIP_NO = mipNo);