0

如何从“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。但是当我传递“四”时,它显示错误,例如 ORA-01427 单行子查询返回多行。我该如何解决这个问题?

4

3 回答 3

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'));

这是一个 sqlfiddle 演示

于 2013-07-24T11:26:57.680 回答
0

您收到 ORA-01427 错误,因为在您的 else 部分中的选择查询中返回不止一行。

以下查询将为您提供帮助:

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
于 2013-07-24T10:55:03.010 回答
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
于 2013-07-24T13:40:27.273 回答