Plsql程序使用控制语句

时间:2016-07-21 03:21:47

标签: oracle plsql

我有一个像“Emp_Info”这样的表,对于下面的输出,我使用了“两个”SELECT语句,我希望使用一个SELECT SELECT STATEMENT工作。

select count(*) into cnt 
from emp_info t
where upper(trim(t.email)) = upper(trim(field1value))
    and t.company_id = companyId;

select 
(case when cnt1 > 0 then 'YES' else 'NO' end) into val_acc 
from (
    select count(*) cnt1 from 
    emp_info t 
    where upper(trim(t.email)) = upper(trim(field1value))
        and (t.account_expiry_dt is null or t.account_expiry_dt >= sysdate)
        and t.company_id = companyId
);

if cnt = 0 then 
    raise_application_error(-20002, 'User does not exist');
elsif cnt > 1 then
    raise_application_error(-20003, 'Duplicate records found');
elsif val_acc = 'NO' then  
    raise_application_error(-20004, 'Account has expired');
else
    /* some logic */
end if;
end;

1 个答案:

答案 0 :(得分:0)

可能对您有所帮助

select count(*),
       CASE WHEN
        sum( CASE when t.account_expiry_dt is null or t.account_expiry_dt >= sysdate then 1 else 0 end) > 0 then 'YES' 
          else 'NO' 
       END
  into cnt, val_acc
  from emp_info t
 where upper(trim(t.email)) = upper(trim(field1value))
   and t.company_id = companyId;

简单演示:

with emp_info(login,username) as (select 'test', 'test' from dual union
                                  select 'TEst', 'TEst' from dual )

select count(*) emp_count,
       CASE when sum( CASE WHEN login like '%st%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag,
       sum( CASE WHEN login like '%st%' then 1 else 0 end) emp_flag_count,
       CASE when sum( CASE WHEN login like '%te%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag1,
       sum( CASE WHEN login like '%te%' then 1 else 0 end) emp_flag_count1,
       CASE when sum( CASE WHEN login like '%TE%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag2,
       sum( CASE WHEN login like '%TE%' then 1 else 0 end) emp_flag_count2
  from emp_info

回答是否有帮助