Oracle CASE在Else子句中

时间:2017-01-03 16:14:23

标签: sql oracle

在Oracle下面,查询单列工作正常,但当我尝试将多列放入时失败,实际上我的要求是退出SQL查询,如果特定表中没有记录及其复杂的内部查询(下面是简化问题)我不能使用PL / SQL块,请建议。

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
  

第1行的错误:
   ORA-00907:缺少右括号

2 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

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

答案 1 :(得分:0)

这应该有效:

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;

我怀疑如果count(*)表很大,将bl1_charge_adj更改为其他内容会更好。