Oracle - 如何从CASE返回多条记录

时间:2013-07-24 10:32:46

标签: oracle oracle-sqldeveloper

如何从“CASE DEFAULT”返回多个记录。

例如:

Master.COLUMN1 IN (CASE '@InputString'
                    WHEN 'One' THEN 1
                    WHEN 'Two' THEN 2
                    WHEN 'THREE' THEN 3
                    ELSE (SELECT NUM_BER FROM TABLE1 WHERE COLUMN2 LIKE '%@InputString%')
                   END)

我尝试传递One并返回1.但是当我传递'four'时,它显示错误,如ORA-01427单行子查询返回多行。我怎么解决这个问题?

3 个答案:

答案 0 :(得分:2)

你可以这样试试:

column1 in (CASE '@InputString'
                    WHEN 'One' THEN 1
                    WHEN 'Two' THEN 2
                    WHEN 'THREE' THEN 3                    
                   END)
OR (column1 in (SELECT NUM_BER FROM TABLE1 WHERE COLUMN2 LIKE '%@InputString%')
    and '@InputString' not in ('One', 'Two', 'THREE'));

Here is a sqlfiddle demo

答案 1 :(得分:0)

您收到ORA-01427错误,因为在您的其他部分的select查询中返回多行。

以下查询可以帮助您:

WITH t AS
     (SELECT 1 string FROM dual
     UNION
     SELECT 2 string FROM dual
     UNION
     SELECT 1234 string FROM dual
     )
SELECT * FROM t
where t.string in ( case 'one' when 'one' then 1 else (select 1234 from dual) end);

output
_____
1

WITH t AS
     (SELECT 1 string FROM dual
     UNION
     SELECT 2 string FROM dual
     UNION
     SELECT 1234 string FROM dual
     )
SELECT * FROM t
where t.string in ( case 'two' when 'one' then 1 else (select 1234 from dual) end);

output
------
1234

答案 2 :(得分:0)

您可以使用decode

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2     select 'A' as string from dual union all
  3     select 'B' from dual union all
  4     select 'C' from dual union all
  5     select 'D' from dual
  6  )
  7  select     string , decode(string, 'A',1,'B',2,3) as string_out
  8* from       x
SQL> /

S STRING_OUT
- ----------
A          1
B          2
C          3
D          3