Oracle循环

本文详细介绍了PL/SQL中的四种循环语句:for、while、loop和goto的使用方法及实例。for循环用于固定次数的迭代,while循环根据条件进行循环,loop循环则在满足条件时退出,goto语句用于跳转到指定标签处继续执行。

原文地址:https://www.cnblogs.com/msx-2018-begin/p/8855471.html,借用原文,在这做下笔记。

1、for语句

declare
begin
   for i in 2..10 Loop  -- 2..10,从2循环到10
     dbms_output.put_line('结果i是:'||i);
  end loop;
end;

2、 while语句

declare
x number;
begin
 x := 5 ;
 while x>0 loop
   x := x-1; -- 循环的每次处理
 if x = 3 then
   return; -- 跳出循环
 else
    dbms_output.put_line('x的值是'||x);
   end if;
 end loop; 
end;

3、 loop语句

declare
x number;
begin
  x:=0;
  loop
    x := x+1;
    exit when x >5 ; -- x大于5是终止
     dbms_output.put_line('结果x是two:'||x);
  end loop;
  end;

4、 goto语句

declare
x number;
begin
  x:=10; --定义的初始值
  <<repeat_loop>> --循环点
  x:= x-2; -- 循环的处理条件
  dbms_output.put_line('结果x是:'||x); -- 循环一次打印一次
  if x>0 then
    GOTO repeat_loop; -- 再次循环
 
   else if x<0 then
     dbms_output.put_line('数值是x:'||x);
     end if;
  end if;
end;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值