`
as619864232
  • 浏览: 320596 次
社区版块
存档分类
最新评论

oracle 中的循环

阅读更多

一、LOOP 循环是最简单的循环,也是无限循环,只能用 EXIT 终止。

 

declare
  starts number:=1;
  counts number:=20;
begin
  loop
    dbms_output.put_line(starts);
    starts:=starts+1;
    exit when starts>=counts;
  end loop;
end;
--输出1-19

 

二、WHILE 循环,判断条件,成立接着走循环体。

 

declare
  starts number:=1;
  counts number:=20;
begin
  while starts<counts loop
    dbms_output.put_line(starts);
    starts:=starts+1;
  end loop;
end;
--同样输出1-19

 

三、FOR 循环,确定循环次数,并指定下限和上限,然后递增或递减,默认递增,若加 REVERSE 关键字,则递减。 

 

declare
  starts number:=1;
  counts number:=20;
begin
  for i in starts..counts loop
    dbms_output.put_line(i);
  end loop;
end;
--输出1-20

declare
  starts number:=1;
  counts number:=20;
begin
  for i in REVERSE starts..counts loop
    dbms_output.put_line(i);
  end loop;
end;
--输出20-1
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics