搞了快一个月的oracle了,天天在谷歌+百度的大力搜索支持下,稍微学会了一点点。
今天下班作个小结:
主要有如下五种循环:ExitWhen、Loop、While、For(普通循环)、For(游标循环),下面举例一一说明(均为存储过程)。sql
一、ExitWhen循环:oracle
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;
二、Loop循环:ide
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;
三、While循环:oop
create or replace procedure proc_test_while is i number; begin i:=0; while i<5 loop i:=i+1; dbms_output.put_line(i); end loop; end proc_test_while;
四、For普通循环:测试
create or replace procedure proc_test_for is i number; begin i:=0; for i in 1..5 loop dbms_output.put_line(i); end loop; end proc_test_for;
五、For游标循环:spa
create or replace procedure proc_test_cursor is userRow test%rowtype; cursor userRows is select * from test; begin for userRow in userRows loop dbms_output.put_line(userRow.id||','||userRow.Name||','||userRows%rowcount); end loop; end proc_test_cursor;
上面所示为存储过程相应代码,你能够经过以下方式进行测试:orm
进入pl/sql,执行文件->新建->程序窗口->空白,拷贝以上各段代码,到pl/sql空白窗口中,安F8执行编译。server
再执行文件->新建->命令窗口进入命令窗口执行一下setserveroutputon这句代码,而后,输入exec相应存储过程,ok。ci
第5中循环要求新建一个名为test的表字段id、name,插入几条数据,进行测试便可。it