案例嵌套案例陈述

时间:2014-07-08 08:41:05

标签: sql oracle

您好我在嵌套的case语句中添加另一组条件时遇到问题。我尝试了几种方式重新格式化语句并放置" END"在不同的地方,但我似乎无法通过ORA-00909:无效的参数数量错误。 我以为我每个CASE只需要一个END,并且END需要在其他之前结束这个条件。那是错的吗?如果我注释掉最后的其他条件,它可以正常工作。

根据下面的评论,我试图理顺我的代码并设置相关的else和end语句,但我仍然得到同样的错误。基本上应该有5组条件。

    CASE 
    WHEN IND0.M_COM_FUT <>0 AND IND0.M_COM_FUT = IND5.M_COM_IND 
    THEN 
        CASE    WHEN IND5.M_COM_IND = CMIND5.M_REFERENCE 
                THEN
                    CASE    WHEN CMIND5.M_PHYSICAL = PHYS5.M_REFERENCE 
                            THEN PHYS5.M_LABEL 
                    END 
        END 
    ELSE
        CASE    WHEN   IND0.M_UNDRL = IND3.M_INDEX AND IND3.M_IND_LAB LIKE '%NBY%' AND IND3.M_COM_NBY_T = 2 
                THEN
                    CASE    WHEN  IND3.M_COM_FUT = IND6.M_COM_IND 
                            THEN 
                                CASE    WHEN IND6.M_COM_IND = CMIND6.M_REFERENCE 
                                        THEN
                                            CASE    WHEN CMIND6.M_PHYSICAL = PHYS6.M_REFERENCE 
                                                    THEN PHYS6.M_LABEL 
                                            END 
                                END 
                    END
          END          
       ELSE
            CASE    WHEN IND0.M_UNDRL = IND3.M_INDEX 
                    THEN
                        CASE    WHEN IND3.M_COM_IND = CMIND3.M_REFERENCE 
                                THEN
                                    CASE    WHEN CMIND3.M_PHYSICAL = PHYS3.M_REFERENCE 
                                            THEN PHYS3.M_LABEL 
                                    END 
                        END                         
            END
        ELSE
            CASE    WHEN   IND0.M_UNDRL = IND3.M_INDEX AND IND3.M_IND_LAB LIKE '%NBY%' AND IND3.M_COM_NBY_T = 0 
                    THEN 
                        CASE    WHEN  IND3.M_COM_FUT = FUT.M_REFERENCE 
                                THEN 
                                    CASE    WHEN FUT.M_QUOT_FWD= QUOT.M_REFERENCE 
                                            THEN
                                                CASE    WHEN QUOT.M_INDEX = IND0.M_INDEX 
                                                        THEN PHYS0.M_LABEL 
                                                END 
                                    END 
                        END 
            END 
         ELSE
            CASE    WHEN IND0.M_IND_LAB= IND0.M_IND_LAB 
                    THEN PHYS0.M_LABEL
            END 

END AS PRODUCT,

2 个答案:

答案 0 :(得分:0)

如果您更好地格式化代码,您应该会发现它有重复的else并且错过了一些end

CASE 
    WHEN IND0.M_COM_FUT <>0 AND IND0.M_COM_FUT = IND5.M_COM_IND 
    THEN 
        CASE    WHEN IND5.M_COM_IND = CMIND5.M_REFERENCE 
                THEN
                    CASE    WHEN CMIND5.M_PHYSICAL = PHYS5.M_REFERENCE 
                            THEN PHYS5.M_LABEL 
                    END 
        END 
    ELSE
        CASE    WHEN   IND0.M_UNDRL = IND3.M_INDEX AND IND3.M_IND_LAB LIKE '%NBY%' AND IND3.M_COM_NBY_T = 2 
                THEN
                    CASE    WHEN  IND3.M_COM_FUT = IND6.M_COM_IND 
                            THEN 
                                CASE    WHEN IND6.M_COM_IND = CMIND6.M_REFERENCE 
                                        THEN
                                            CASE    WHEN CMIND6.M_PHYSICAL = PHYS6.M_REFERENCE 
                                                    THEN PHYS6.M_LABEL 
                                            END 
                                END 
                    END 
                ELSE
                    CASE    WHEN IND0.M_UNDRL = IND3.M_INDEX 
                            THEN
                                CASE    WHEN IND3.M_COM_IND = CMIND3.M_REFERENCE 
                                        THEN
                                            CASE    WHEN CMIND3.M_PHYSICAL = PHYS3.M_REFERENCE 
                                                    THEN PHYS3.M_LABEL 
                                            END 
                                END 
                            ELSE
                                CASE    WHEN IND0.M_IND_LAB= IND0.M_IND_LAB 
                                        THEN PHYS0.M_LABEL
                                END 
                            END 
                    END
                ELSE
                    CASE    WHEN   IND0.M_UNDRL = IND3.M_INDEX AND IND3.M_IND_LAB LIKE '%NBY%' AND IND3.M_COM_NBY_T = 0 
                            THEN 
                                CASE    WHEN  IND3.M_COM_FUT = FUT.M_REFERENCE 
                                        THEN 
                                            CASE    WHEN FUT.M_QUOT_FWD= QUOT.M_REFERENCE 
                                                    THEN
                                                        CASE    WHEN QUOT.M_INDEX = IND0.M_INDEX 
                                                                THEN PHYS0.M_LABEL 
                                                        END 
                                            END 
                                END 
                    END 
END AS PRODUCT,

答案 1 :(得分:0)

试试这个开始吧。我不确定你的逻辑是什么,但至少应该运行并根据你的代码给你我认为你正在寻找的结果。

CASE
  WHEN IND0.M_COM_FUT <> 0
   AND IND0.M_COM_FUT = IND5.M_COM_IND
   AND IND5.M_COM_IND = CMIND5.M_REFERENCE
   AND CMIND5.M_PHYSICAL = PHYS5.M_REFERENCE
    THEN PHYS5.M_LABEL  
  WHEN IND0.M_UNDRL = IND3.M_INDEX
   AND IND3.M_IND_LAB LIKE '%NBY%'
   AND IND3.M_COM_NBY_T = 2
   AND IND3.M_COM_FUT = IND6.M_COM_IND
   AND IND6.M_COM_IND = CMIND6.M_REFERENCETHEN
   AND CMIND6.M_PHYSICAL = PHYS6.M_REFERENCE
    THEN PHYS6.M_LABEL 
  WHEN IND0.M_UNDRL = IND3.M_INDEX
   AND IND3.M_COM_IND = CMIND3.M_REFERENCE
   AND CMIND3.M_PHYSICAL = PHYS3.M_REFERENCE
    THEN PHYS3.M_LABEL 
  WHEN IND0.M_UNDRL = IND3.M_INDEX
   AND IND3.M_IND_LAB LIKE '%NBY%'
   AND IND3.M_COM_NBY_T = 0 
   AND IND3.M_COM_FUT = FUT.M_REFERENCE
   AND FUT.M_QUOT_FWD= QUOT.M_REFERENCE
   AND QUOT.M_INDEX = IND0.M_INDEX
    THEN PHYS0.M_LABEL 
  WHEN IND0.M_IND_LAB= IND0.M_IND_LAB
    THEN PHYS0.M_LABEL
END

如果WHEN语句不匹配,它会自动转到下一个WHEN。

匹配的第一个WHEN语句将忽略所有其他WHEN语句,即使其下面的一个或多个WHEN语句也匹配。

如果没有WHEN语句匹配,则可以在最后一个WHEN之后使用ELSE一次返回默认值。