oracle case

jss001發表於2009-03-06

/******************************/

case語句,求助高手

幾個when分支執行相同操作,怎麼合併到一塊
比如:
case selector
when a then A;
when b then A;
when c then B;
when d then B;
when e then C
end case;

/**************************************/

set serverout on

create table test (id varchar2(10));
insert into test values('1');
insert into test values('2');
insert into test values('3');
insert into test values('4');
insert into test values('5');
commit;

declare
cursor cur_test is select id from test;
begin
for row_cur in cur_test loop
case when row_cur.id='1' or row_cur.id='2' then
dbms_output.put_line('A');
when row_cur.id='3' or row_cur.id='4' then
dbms_output.put_line('B');
else
dbms_output.put_line('C');
end case;
end loop;
end;
/


declare
cursor cur_test is select id from test;
begin
for row_cur in cur_test loop
dbms_output.put_line(
case when row_cur.id='1' or row_cur.id='2' then
'A'
when row_cur.id='3' or row_cur.id='4' then
'B'
else
'C'
end);
end loop;
end;
/


declare
cursor cur_test is
select case
when id='1' or id='2' then 'A'
when id='3' or id='4' then 'B'
else 'C' end id
from test;
begin
for row_cur in cur_test loop
dbms_output.put_line(row_cur.id);
end loop;
end;
/

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18921899/viewspace-1016972/,如需轉載,請註明出處,否則將追究法律責任。

相關文章