简体   繁体   中英

Oracle CASE failing with multiple columns in Else clause

In Oracle below query is working fine with single column but when I tried to put multiple columns it fails, Actually my requirement is to Exit SQL query if there is no records in particular table and its complex inner query (below is simplified problem description) and I cannot use PL/SQL block, please suggest.

SQL> select 
         case when (select count(*) from bl1_charge_adj where rownum=1) = '1' then ( select customer_id  from customer) 
         else 44 END as ee
     from dual;

EE
----------
44

SQL> select 
         case when (select count(*) from bl1_charge_adj where rownum=1) = '1' then ( select customer_id,DL_UPDATE_STAMP from customer) 
         else (1,2) END 
     from dual

ERROR at line 1:
ORA-00907: missing right parenthesis

I think you want something like this:

select customer_id, DL_UPDATE_STAMP
from customer
where exists (select 1 from bl1_charge_adj)
union all
select 1, 2
from dual
where not exists (select 1 from bl1_charge_adj);

This should work:

select nvl(c.customer_id, 1), nvl(c.DL_UPDATE_STAMP, 2)
from ( select count(*) as c from bl1_charge_adj where rownum=1) d
left outer join customer c on d.c = 1;

I suspect it would be better done by changing the count(*) to something else if the bl1_charge_adj table is big.

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