返回多行时的SQL查询

时间:2011-08-19 18:15:18

标签: sql oracle

我正在尝试使用条件进行查询,以查看我将显示的列表,但我遇到此错误ORA-01427: single-row subquery returns more than one row

查询是这样的:

select
CASE WHEN action_type like 'Trigger Severity' THEN (select cast(SEVERITY as varchar2(255)) name from SURV_TRIGGER_SEVERITY_LIST)
     WHEN action_type like 'Host Group'  then (select cast(name as varchar2(255)) name from Surv_List.groups)
     WHEN action_type like 'Host'  then (select cast(name as varchar2(255)) name from tn_tree)
    END display_value
from surv_action_type_list
where id = 0

是否可以在一个案例条件中调用一行多一行的查询?

3 个答案:

答案 0 :(得分:2)

我会在多个步骤中执行此操作。获取操作类型,然后发出相应的查询。无论你在前端还是在存储过程中都有这种逻辑,你都可以依赖很多其他东西。

如果您绝对需要这样做,那么您可以尝试这样的事情:

SELECT
    SQ.display_value
FROM
    surv_action_type_list SATL
INNER JOIN
    (
    SELECT
        'Trigger Severity' action_type,
        CAST(severity AS VARCHAR2(255)) display_value
    FROM
        SURV_TRIGGER_SEVERITY_LIST
    UNION ALL
    SELECT
        'Host Group' action_type,
        CAST(name AS VARCHAR2(255) display_value
    FROM
        Surv_List.groups
    UNION ALL
    SELECT
        'Host' action_type,
        CAST(name AS VARCHAR2(255) display_value
    FROM
        tn_tree
    ) SQ ON
    SQ.action_type = SATL.action_type
WHERE
    SATL.id = 0

答案 1 :(得分:0)

您有3个子查询。

1. select cast(SEVERITY as varchar2(255)) name from SURV_TRIGGER_SEVERITY_LIST
2. select cast(name as varchar2(255)) name from Surv_List.groups
3. select cast(name as varchar2(255)) name from tn_tree

每个人必须返回0或1行但不能更多。

答案 2 :(得分:0)

没有。您的子查询应该只返回一个值(只有一行和一列),因为您将在一行上显示它。

由于您使用上面的查询将值显示为一列,因此您的意图似乎只是获取一个值。

select
CASE WHEN action_type like 'Trigger Severity' THEN (select cast(SEVERITY as varchar2(255)) name from SURV_TRIGGER_SEVERITY_LIST)
     WHEN action_type like 'Host Group'  then (select cast(name as varchar2(255)) name from Surv_List.groups)
     WHEN action_type like 'Host'  then (select cast(name as varchar2(255)) name from tn_tree)
    END display_value
from surv_action_type_list
where id = 0

是否有遗漏链接此ID以表示严重性列表? 通常这样的查询在子查询中会有一个条件......比如..

 select
     CASE WHEN action_type like 'Trigger Severity' 
             THEN (select cast(SEVERITY as varchar2(255)) name 
                      **from SURV_TRIGGER_SEVERITY_LIST trglst
                      where trglst.name = lst.severity_name**
     -----
     ---

         END display_value
     from surv_action_type_list lst
    where id = 0