ORA-00913:嵌套大小的值过多

时间:2015-05-07 15:50:32

标签: sql oracle oracle11g oracle-apex

我目前正在进行第一年的工作,使用Oracle Apex构建数据库,并尝试创建一个包含字段的报告来过滤显示的表格。要使用2个参数(在这种情况下,牙医预约的日期和执行所述预约的工作人员)这样做,我使用嵌套的案例来检查每个字段。我收到了错误:

ORA-06550:第6行,第23栏:ORA-00913:值太多。

我,我的同学和我的导师都没有看到如何解决它。希望有人在这里看到我的错误。

这是我的代码:

SELECT 
CASE :P5_APPOINTMENT_DATE
    WHEN (NULL)
        THEN CASE :P5_STAFF
            WHEN (NULL)
                THEN (select
                      APPOINTMENT_ID AS "Appointment ID",
                      Patient.Surname AS "Patient Surname",
                      Patient.Forename AS "Patient Forename",
                      Staff.Surname AS "Staff Surname",
                      A_DATE AS "Date"
                      FROM Appointment
                      Inner Join Patient ON Patient.Patient_ID = Appointment.Patient_ID
                      Inner Join Staff ON Staff.Staff_ID = Appointment.Staff_ID)

                ELSE (select
                      APPOINTMENT_ID AS "Appointment ID",
                      Patient.Surname AS "Patient Surname",
                      Patient.Forename AS "Patient Forename",
                      Staff.Surname AS "Staff Surname",
                      A_DATE AS "Date"
                      FROM Appointment
                      Inner Join Patient ON Patient.Patient_ID = Appointment.Patient_ID
                      Inner Join Staff ON Staff.Staff_ID = Appointment.Staff_ID
                      Where STAFF.STAFF_ID = :P5_STAFF) 
                END 


        ELSE CASE :P5_STAFF
            WHEN (NULL)
                THEN (select
                      APPOINTMENT_ID AS "Appointment ID",
                      Patient.Surname AS "Patient Surname",
                      Patient.Forename AS "Patient Forename",
                      Staff.Surname AS "Staff Surname",
                      A_DATE AS "Date"
                      FROM Appointment
                      Inner Join Patient ON Patient.Patient_ID = Appointment.Patient_ID
                      Inner Join Staff ON Staff.Staff_ID = Appointment.Staff_ID
                      Where A_DATE = :P5_APPOINTMENT_DATE)
                ELSE (select
                      APPOINTMENT_ID AS "Appointment ID",
                      Patient.Surname AS "Patient Surname",
                      Patient.Forename AS "Patient Forename",
                      Staff.Surname AS "Staff Surname",
                      A_DATE AS "Date"
                      FROM Appointment
                      Inner Join Patient ON Patient.Patient_ID = Appointment.Patient_ID
                      Inner Join Staff ON Staff.Staff_ID = Appointment.Staff_ID
                      Where A_DATE = :P5_APPOINTMENT_DATE AND STAFF.Surname = :P5_STAFF)
                END

        END
        FROM APPOINTMENT

1 个答案:

答案 0 :(得分:1)

正如Gordon Linoff在评论中所说,case会返回一个值。由于大部分子查询是相同的,并且您只是改变过滤条件,因此可以将case移动到where子句;除了这里你甚至不需要一个案例,你可以用or获得同样的效果,例如:

SELECT APPOINTMENT_ID AS "Appointment ID",
  Patient.Surname AS "Patient Surname",
  Patient.Forename AS "Patient Forename",
  Staff.Surname AS "Staff Surname",
  A_Date AS "Date"
FROM Appointment
JOIN Patient ON Patient.Patient_ID = Appointment.Patient_ID
JOIN Staff ON Staff.Staff_ID = Appointment.Staff_ID
WHERE (:P5_APPOINTMENT_DATE IS NULL OR A_Date = :P5_APPOINTMENT_DATE)
AND (:P5_STAFF IS NULL OR STAFF.Surname = :P5_STAFF);

如果两个绑定变量都为null,您将获得所有记录;如果其中一个或两个都不为null,那么您将获得匹配值的子集。这似乎是你想要的 - 四种可能的结果取决于设置两个变量中的哪一个。