oracle控制语句if else,loop等

来源:互联网 发布:c语言对数ln 编辑:程序博客网 时间:2024/06/13 01:50
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;
原创粉丝点击