您的位置:首页 > 数据库 > Oracle

oracle控制语句if else,loop等

2017-09-12 23:37 555 查看
oracle中的控制语句

1.条件语句 

if xxx then

else

  xxx;

end if;

---------------------

if xxx then

elsif xxx then

  xxx;

else

  xxx;

end if;

---------------------

case

  when xxx then

    xxx;

  when xxx then

    xxx;

  else

end case;  

---------------------

case xxx

  when xxx then

    xxx;

  when xxx then

    xxx;

  else

end case;  

---------------------

select student_name,

       sum(case when subject='数学' then score else 0 end) 数学,

       sum(case when subject='语文' then score else 0 end) 语文,

       sum(case when subject='外语' then score else 0 end) 外语

from scores

group by student_name;

2.循环语句

【loop】

--exit when

declare

  v_rlt number(8):=-3;

begin

   <<fst_loop>>

   loop

     dbms_output.put_line('v_rlt = '||v_rlt);

     v_rlt:=v_rlt+1;

     exit fst_loop when v_rlt > 3;

   end loop;

     dbms_output.put_line('LOOP循环已经结束!');

end;

--结果

v_rlt = -3

v_rlt = -2

v_rlt = -1

v_rlt = 0

v_rlt = 1

v_rlt = 2

v_rlt = 3

LOOP循环已经结束!



--if exit


declare

  v_rlt number(8):=-3;

begin

   <<fst_loop>>

   loop

     dbms_output.put_line('v_rlt = '||v_rlt);

     v_rlt:=v_rlt+1;

     if v_rlt > 3 then

        dbms_output.put_line('变量的值已经大于3,当前值为'||v_rlt);

     exit fst_loop;

     end if;

   end loop fst_loop;

     dbms_output.put_line('LOOP循环已经结束!');

end;


【while...loop】


--while...loop

declare

  v_rlt number(8):=-3;

begin

   <<while_loop>>

   while(v_rlt < 4)

   loop

     dbms_output.put_line('v_rlt = '||v_rlt);

     v_rlt:=v_rlt+1;

   end loop while_loop;

     dbms_output.put_line('WHILE循环已经结束!');

end;


【for...loop】


--for..loop

begin

   for v_rlt in -3..3 loop

     dbms_output.put_line('v_rlt = '||v_rlt);

   end loop;

     dbms_output.put_line('FOR循环已经结束!');

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 dba sql oracle plsql