简体   繁体   中英

Using decode in where clause

I have a DECODE in my WHERE clause like this:

Where id = decode('&flag','Yes',(Select id from tab where id > 10),0)

This code works if the subquery returns one post. If I returns several I get an error like, ORA-01427, "Single-row subquery returns more than one row"

I've tried to change the '=' to an 'in' but I still get the same error!

Any ideas?

extended example:

WHERE Dop_id = (DECODE ('&prep_flag', 'Yes', 
    (SELECT Dop_id FROM 
                   ( SELECT DOP_id, name FROM TABLE)
                    WHERE name IS NOT NULL) 
 , Dop_id))

as mention this works if the select statmen returns on row, and not several.

Assuming decode is only expecting a single value where you have Select id from tab where id > 10 I would try moving the select outside of decode:

WHERE id IN (
    SELECT decode('&flag', 'Yes', id, 0)
    FROM tab
    WHERE id > 10
)
WHERE Dop_id IN 
      ( CASE &flag
        WHEN 'Yes'
        THEN (SELECT Dop_id 
                FROM TABLE
               WHERE name IS NOT NULL)
        ELSE Dop_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM