asfsd 发表于 2018-9-14 06:02:44

oracle 循环语句总结

  搞了快一个月的oracle了,每天在谷歌+百度的大力搜索支持下,稍微学会了一点点。
  今天下班做个小结:
  主要有以下五种循环:ExitWhen、Loop、While、For(普通循环)、For(游标循环),下面举例一一说明(均为存储过程)。
  1、ExitWhen循环:
create or replace procedure proc_test_exit_when is  
i number;
  
begin
  
i:=0;
  
LOOP
  
Exit When(i>5);
  
Dbms_Output.put_line(i);
  
i:=i+1;
  
END LOOP;
  
end proc_test_exit_when;
  

  2、Loop循环:
create or replace procedure proc_test_loop is  
i number;
  
begin
  
i:=0;
  
loop
  
i:=i+1;
  
dbms_output.put_line(i);
  
if i>5 then
  
exit;
  
end if;
  
end loop;
  
end proc_test_loop;
  

  3、While循环:
create or replace procedure proc_test_while is  
i number;
  
begin
  
i:=0;
  
while i新建->程序窗口->空白,拷贝以上各段代码,到pl/sql空白窗口中,安F8执行编译。
  再执行文件->新建->命令窗口进入命令窗口执行一下setserveroutputon这句代码,然后,输入exec相应存储过程,ok。
  第5中循环要求新建一个名为test的表字段id、name,插入几条数据,进行测试即可。


页: [1]
查看完整版本: oracle 循环语句总结