ORA-00907: 缺少右括号 - 当我将 Case 语句放入 where 子句时出现以下错误

ORA-00907: missing right parenthesis - Following error is coming when i am putting Case statement in where clause

$ 以下代码仅突出显示 BI Publisher 查询的 where 子句,其中我有一个输入参数:P_COUNTRY,通过它我收到的值是 PH/MY/CN 在此基础上,我必须放置where子句。

WHERE     PRG.PAYROLL_RELATIONSHIP_ID = PEU.PAYROLL_RELATIONSHIP_ID
     AND PEU.ELEMENT_ENTRY_ID = PEE.ELEMENT_ENTRY_ID
     AND PEE.ELEMENT_TYPE_ID = PET.ELEMENT_TYPE_ID
     AND PRG.ASSIGNMENT_ID = PAAF.ASSIGNMENT_ID
     AND PPSV.PERSON_ID = PPNF.PERSON_ID
     AND PPSV.PERSON_ID = PAAF.PERSON_ID
     --AND  PPNF.PERSON_ID  = CCPP.PERSON_ID
     AND PAAF.PRIMARY_ASSIGNMENT_FLAG = 'Y'
     AND PAAF.ASSIGNMENT_TYPE = 'E'
     AND PAAF.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
     AND PPOS.PERSON_ID = PAAF.PERSON_ID
     AND PPOS.LEGAL_ENTITY_ID = PLE.ORGANIZATION_ID
     AND PPNF.NAME_TYPE = 'GLOBAL'
     AND    (CASE   WHEN (:P_Country = 'PH') THEN (UPPER(PET.ELEMENT_NAME) IN ('PHCTC EXT'))
                    WHEN (:P_Country = 'MY') THEN (UPPER(PET.ELEMENT_NAME) IN ('MYCTC EXT'))
                    WHEN (:P_Country = 'CN') THEN (UPPER(PET.ELEMENT_NAME) IN ('CNCTC EXT'))
            END)
     AND PAAF.GRADE_ID = PG.GRADE_ID

你想要的更像是:

WHERE     PRG.PAYROLL_RELATIONSHIP_ID = PEU.PAYROLL_RELATIONSHIP_ID
     AND PEU.ELEMENT_ENTRY_ID = PEE.ELEMENT_ENTRY_ID
     AND PEE.ELEMENT_TYPE_ID = PET.ELEMENT_TYPE_ID
     AND PRG.ASSIGNMENT_ID = PAAF.ASSIGNMENT_ID
     AND PPSV.PERSON_ID = PPNF.PERSON_ID
     AND PPSV.PERSON_ID = PAAF.PERSON_ID
     --AND  PPNF.PERSON_ID  = CCPP.PERSON_ID
     AND PAAF.PRIMARY_ASSIGNMENT_FLAG = 'Y'
     AND PAAF.ASSIGNMENT_TYPE = 'E'
     AND PAAF.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
     AND PPOS.PERSON_ID = PAAF.PERSON_ID
     AND PPOS.LEGAL_ENTITY_ID = PLE.ORGANIZATION_ID
     AND PPNF.NAME_TYPE = 'GLOBAL'
     AND UPPER(PET.ELEMENT_NAME) =
            CASE   WHEN :P_Country = 'PH' THEN 'PHCTC EXT'
                   WHEN :P_Country = 'MY' THEN 'MYCTC EXT'
                   WHEN :P_Country = 'CN' THEN 'CNCTC EXT'
            END
     AND PAAF.GRADE_ID = PG.GRADE_ID

不需要您使用的所有括号,当您只比较一个值时也不需要使用 IN

另一种选择是取消 CASE 并使用一系列分组的条件子句:

WHERE     PRG.PAYROLL_RELATIONSHIP_ID = PEU.PAYROLL_RELATIONSHIP_ID
     AND PEU.ELEMENT_ENTRY_ID = PEE.ELEMENT_ENTRY_ID
     AND PEE.ELEMENT_TYPE_ID = PET.ELEMENT_TYPE_ID
     AND PRG.ASSIGNMENT_ID = PAAF.ASSIGNMENT_ID
     AND PPSV.PERSON_ID = PPNF.PERSON_ID
     AND PPSV.PERSON_ID = PAAF.PERSON_ID
     --AND  PPNF.PERSON_ID  = CCPP.PERSON_ID
     AND PAAF.PRIMARY_ASSIGNMENT_FLAG = 'Y'
     AND PAAF.ASSIGNMENT_TYPE = 'E'
     AND PAAF.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
     AND PPOS.PERSON_ID = PAAF.PERSON_ID
     AND PPOS.LEGAL_ENTITY_ID = PLE.ORGANIZATION_ID
     AND PPNF.NAME_TYPE = 'GLOBAL'
     AND (
        (:P_Country = 'PH' AND UPPER(PET.ELEMENT_NAME) = 'PHCTC EXT') OR
        (:P_Country = 'MY' AND UPPER(PET.ELEMENT_NAME) = 'MYCTC EXT') OR
        (:P_Country = 'CN' AND UPPER(PET.ELEMENT_NAME) = 'CNCTC EXT')
     )
     AND PAAF.GRADE_ID = PG.GRADE_ID

Case 表达式不太适合 Where 子句

您正在使用 searched case expression。 case 表达式的结果组件不接受布尔值。

现有条件如下:

   AND    (CASE   WHEN (:P_Country = 'PH') THEN (UPPER(PET.ELEMENT_NAME) IN ('PHCTC EXT'))
                    WHEN (:P_Country = 'MY') THEN (UPPER(PET.ELEMENT_NAME) IN ('MYCTC EXT'))
                    WHEN (:P_Country = 'CN') THEN (UPPER(PET.ELEMENT_NAME) IN ('CNCTC EXT'))
            END)

我将您的案例陈述翻译成以下内容:

正在检查绑定变量 :P_Country,看它是否在 PET.ELEMENT_NAME 中。更重要的是,它只是 ELEMENT_NAME.

的第一部分

在SQL中,我写:

AND UPPER(PET.ELEMENT_NAME) = :P_Country ||'CTC EXT'

最好知道 ELEMENT_NAME 是全上、下、正或混合。可以修改绑定变量的大小写以对应 ELEMENT_NAME.

你会得到更好的表现。

例如,如果我们知道 ELEMENT_NAME 总是大写,我们可以这样做:

AND PET.ELEMENT_NAME = UPPER(:P_Country) ||'CTC EXT'