oracle存储过程--流程控制(条件判断和循环遍历)

*流程控制*

*条件分支(判断)*

语法:

Begin
  If 条件1 then 执行1
  Elsif  条件2  then 执行2
  Else  执行3
  End if;
End;

If 条件1 then 执行1

Elsif 条件2 then 执行2 Else 执行3 End if;End;

注意:关键字 Elsif

小案例:--判断emp表中的记录** 是否超过20条,10-20之间,或者10条以下

**--判断emp表中的记录** **是否超过20条,10-20之间,或者10条以下**

declare

 **--声明变量接收emp表中的记录数**

 v_count number;
begin

 select count(1) into v_count from emp;

 if v_count > 20 then

  dbms_output.put_line('emp表中的记录数超过了20条为:' || v_count);

 elsif v_count >= 10 then

  dbms_output.put_line('emp表中的记录数为10-20之间为:' || v_count);

 else

  dbms_output.put_line('emp表中的记录数为10条以下为:' || v_count);

 end if;

end;

执行结果:

emp表中的记录数为10-20之间为:14

*循环*

在oracle中有三种循环方式,这里我们不展开,只介绍其中一种:loop循环

语法:

Begin
  Loop
Exit when 退出循环条件
  End loop;
End;

小案例:打印数字1-10

【示例】打印数字1-10
--打印数字1-10
declare
  --声明循环变量并赋初始值
  v_num number  :=1;
begin
 loop
   --退出循环的条件
   exit when v_num >10;
   dbms_output.put_line(v_num);
   --循环变量自增
   v_num :=v_num+1;
 end loop;
end;

执行结果:
1
2
3
4
5
6
7
8
9
10
原文地址:https://www.cnblogs.com/dongyaotou/p/14258809.html