oracle 中循环总结
--while循环控制语句
declare i int := 1; begin while i <= 10 loop dbms_output.put_line('当前循环体内i的值:' || i); i := i + 1; end loop; end;
--for循环控制语句 递增
begin for i in 1 .. 10 loop dbms_output.put_line('当前循环体内i的值:' || i); end loop; end;
--for循环控制语句 递减
begin for i in reverse 1 .. 10 loop dbms_output.put_line('当前循环体内i的值:' || i); end loop; end;
--嵌套循环和标号
declare
result int := 0;
begin
<>
for i in 1 .. 20 loop
<>
for j in 1 .. 10 loop
result := i * j;
dbms_output.put_line('循环时,i=' || i || ',j=' || j || ',result的值:' ||
result);
--当外层循环result值达到200时,跳出整个外层循环
exit outer when result = 200;
--当内层循环result值达到100时,跳出内层循环
exit when result = 100;
end loop inner;
dbms_output.put_line('end inner ,result的值:' || result);
end loop outer;
dbms_output.put_line('end outter ,result的值:' || result);
end;
--游标 for循环
--数据准备 create table test(a varchar2(100)); insert into test values('test1'); insert into test values('test2'); insert into test values('test3'); commit; --测试开始 declare testRow test%rowtype; cursor testRows is select * from test; begin for testRow in testRows loop dbms_output.put_line(testRow.a ||' '|| testRows%rowcount); end loop; end; --数据销毁 drop table test;
--循环插入数据库语句
declare
maxrecords constant int:=1000;
i int :=1;
begin
for i in 1..maxrecords loop
insert into test(a)
values(to_char('test'||i));
end loop;
dbms_output.put_line(' 成功录入数据!');
commit;
end;
不建议使用