使用LOOP循环
declare
v_i number:=1;
begin
loop
dbms_output.put_line('v_i='||v_i);
exit when v_i>=3;
v_i:=v_i+1;
end loop;
end;
/
使用while...loop循环
declare
v_i number:=1;
begin
while(v_i<=3)loop
dbms_output.put_line('v_i='||v_i);
v_i:=v_i+1;
end loop;
end;
/
使用FOR循环
declare
v_i number:=1;
begin
for v_i in 1..3 loop
dbms_output.put_line('v_i='||v_i);
end loop;
end;
/
使用REVERSE循环(倒着输出)
declare
v_i number:=1;
begin
for v_i in reverse 1..3 loop
dbms_output.put_line('v_i='||v_i);
end loop;
end;
/
循环控制语句
使用CONTINUE控制循环操作
declare
v_i number:=1;
begin
for v_i in 1..10 loop
if mod(v_i,2)=0 then
continue;
end if;
dbms_output.put_line('v_i='||v_i);
end loop;
end;
/
使用EXIT退出循环操作
declare
v_i number:=1;
begin
for v_i in 1..10 loop
if v_i=3 then
exit;
end if;
dbms_output.put_line('v_i='||v_i);
end loop;
end;
使用GOTO进行跳转
eclare
v_result number:=1;
begin
for v_result in 1..10 loop
if v_result=2 then
goto endpoint;
end if;
dbms_output.put_line('v_result='||v_result);
end loop;
<<endpoint>>
dbms_output.put_line('THE END...');
dbms_output.put_line('THE END...');
END;
/
在块中使用循环,三种循环模式
declare
v1 number(2):=1;
begin
loop
insert into t1 values(v1);
v1:=v1+1;
exit when v1>10;
end loop;
end;
/
declare
v1 number(2):=1;
begin
while v1<10 loop
insert into t1 values(v1);
v1:=v1+1;
end loop;
end;
/
declare
v1 number;
begin
for v1 in 1..9 loop
insert into t1 values(v1);
end loop;
end;
/