【转】Oracle循环语句总结

Oracle主要有以下五种循环:Exit When、Loop、While、For(普通循环)、For(游标循环),下面举例一一说明(均为存储过程)。

1. Exit When循环:
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<5 loop
i:=i+1;
dbms_output.put_line(i);
end loop;
end proc_test_while;

4. 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;

5. For游标循环:
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;

本文转自:http://www.cnblogs.com/zsdentist/archive/2011/12/04/2276249.html

posted @ 2012-11-01 17:52  先行而后三思  阅读(222)  评论(0编辑  收藏  举报