ORA-00907:缺少右括号

时间:2011-03-30 07:10:10

标签: oracle case ora-00907

SELECT DISTINCT( EMP.EMPLOYEEID ),
    EMP.EMPLOYEECODE,
    EMP.EMPLOYEENAME,
    EMP.HOMEADDRESS,
    DESIG.DESIGNATIONNAME
  FROM HRM_EMPLOYEE EMP,
    COM_DESIGNATION DESIG,
    COM_DEPARTMENT DEPT,
    COM_COMPANY COMP,
    HRM_EMPLOYEEDEPARTMENTS EMPDEPT,
    USR_USERS USRS
  WHERE EMP.EMPLOYEEID = EMPDEPT.EMPLOYEEID AND
    EMP.DESIGNATIONID  = DESIG.DESIGNATIONID AND
    DESIG.DEPARTMENTID = EMPDEPT.DEPARTMENTID AND
    EMP.STATUS IN  (SELECT  STAT STAT
        FROM
          (
          CASE
          When (:status = 0) THEN
            SELECT   1 STAT FROM dual
            UNION ALL
            SELECT   2 STAT FROM dual
          else
            Select :status STAT from dual
          end
          )
          xx
       ) 

其实我的需求是:根据参数传递存放记录。如果tat参数我希望显示所有记录。

2 个答案:

答案 0 :(得分:4)

您不能将CASE语句作为表表达式(除非使用嵌套表类型)。但为什么这么复杂?而不是:

EMP.STATUS IN  (SELECT  STAT STAT
    FROM
      (
      CASE
      When (:status = 0) THEN
        SELECT   1 STAT FROM dual
        UNION ALL
        SELECT   2 STAT FROM dual
      else
        Select :status STAT from dual
      end
      )
      xx
   ) 

写下这个:

(EMP.STATUS IN (1, 2) AND :status = 0) OR
(EMP.STATUS = :status)

答案 1 :(得分:1)

SELECT DISTINCT( EMP.EMPLOYEEID ),
    EMP.EMPLOYEECODE,
    EMP.EMPLOYEENAME,
    EMP.HOMEADDRESS,
    DESIG.DESIGNATIONNAME
  FROM HRM_EMPLOYEE EMP,
    COM_DESIGNATION DESIG,
    COM_DEPARTMENT DEPT,
    COM_COMPANY COMP,
    HRM_EMPLOYEEDEPARTMENTS EMPDEPT,
    USR_USERS USRS
  WHERE EMP.EMPLOYEEID = EMPDEPT.EMPLOYEEID AND
    EMP.DESIGNATIONID  = DESIG.DESIGNATIONID AND
    DESIG.DEPARTMENTID = EMPDEPT.DEPARTMENTID AND
    ( 
     (EMP.STATUS IN (1, 2) and :status = 0)
      or :status <> 0 --This will not filter your status, as I expect you want it to do so
     )