Pregunta

I working with a cursor which has an CASE expression in one of its WHERE-clauses, like so:

SELECT tbl.id
FROM   table1 tbl
WHERE  tbl.id = CASE
                  WHEN [some condition]
                    THEN [a single id number]
                    ELSE ( SELECT tbl2.id
                           FROM table2 tbl2
                           WHERE [some other condition]
                         )
                END

This works well until the sub-select returns multiple rows. If that happens an ORA-01427 exception is thrown. I've tried to remedy this by using the IN or ANY statements, but looks to me like the CASE expression simply is unable to output sets of multiple values.

If possible, how does one create a WHERE clause like this one, wherein the sub-select potentially could return multiple rows?

¿Fue útil?

Solución

How can you compare tbl.id = [multiple rows]?

You need to use the "Exists" or " IN " syntax. For example:

SELECT tbl.id
  FROM table1 tbl
 WHERE ( [some condition] AND tbl.id = [a single id number])
OR ( NOT [some condition] AND tbl.id IN ( SELECT tbl2.id
                                           FROM table2 tbl2
                                          WHERE [some other condition])
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top