Oracle中的for和while循环

本文详细介绍了Oracle中for和while循环的使用方法,包括循环的终止条件、实例演示及与游标的对比,旨在帮助开发者熟练掌握这两种循环结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Oracle中的for和while循环

有两种方式可以实现条件循环

一、for 变量 in 开始数值...结束数值loop    end loop;
二、while 条件loop      end loop;

loop的使用方式:

exit when 语句可以出现在循环代码中的任何位置;

for循环:


begin
    for x inreverse 1..10 loop -- reverse大到小
       DBMS_OUTPUT.PUT_LINE('内:x='||x);
    endloop;
   
   DBMS_OUTPUT.PUT_LINE('endget');
end;

SQL> declare x int;
  2  begin
  3  while x<=10
  4  loop
  5  dbms_output.put_line('x='||x);
  6  end loop;
  7  end;
  8  /

PL/SQL 过程已成功完成。

BEGIN
FOR v_emp IN (select * from emp where deptno=12 )

LOOP

DBMS_OUTPUT.PUT_LIEN(v.emp.empno||'      '||v_emp.ename);

END LOOP;

END;

SQL> set serveroutput on
SQL> begin
  2  >for x in reverse 1..10
  3  >loop
  4  >dbms_output.put_line('x='||x);
  5  >end loop;
  6  >end;
  7  >/
x=10
x=9
x=8
x=7
x=6
x=5
x=4
x=3
x=2
x=1

PL/SQL 过程已成功完成。



 SQL> begin
  2  for x in 1..10
  3  loop
  4  dbms_output.put_line('x='||x);
  5  end loop;
  6  end;
  7  /
x=1
x=2
x=3
x=4
x=5
x=6
x=7
x=8
x=9
x=10

PL/SQL 过程已成功完成。


SQL> begin
  2  for x in reverse 1..10 loop dbms_output.put_line('x='||x);end loop;
  3  end;
  4  /
x=10
x=9
x=8
x=7
x=6
x=5
x=4
x=3
x=2
x=1

PL/SQL 过程已成功完成。


如以下两个例:

1)
Create Or Replace Procedure Count_Number Is
  i Number(10);
  t Number(10);
Begin
  i := 1;
  t := 0;
  Loop
   i:=i+1;
    ---exit wheni=100;
    exit wheni>100;
   t:=t+i;
   Dbms_Output.Put_Line('it''s :' || t);
    endloop;
   ---Dbms_Output.Put_Line('it''s :' || t);
End Count_Number;

2)
Create Or Replace Procedure Count_Number Is
  i Number(10);
  t Number(10);
Begin
  i := 1;
  t := 0;
  Loop
   i:=i+1;
    if i=100then
    exit;
    end if;
   t:=t+i;
   Dbms_Output.Put_Line('it''s :' || t);
    endloop;
   ---Dbms_Output.Put_Line('it''s :' || t);
End Count_Number;
注:以上的结论都是一样的 !

 

以下是一个非常简单的过程,用来熟悉循环的!

1)使用while ....loop ....end loop ;
CreateOr Replace Procedure Count_Number Is
  i Number(10);
  t Number(10);
Begin
  i := 1;
  t := 0;
  while i<=100 loop
  t:=t+i;
  i:=i+1;
  Dbms_Output.Put_Line('it''s :' || t);
  end loop;
   ---Dbms_Output.Put_Line('it''s :' || t);
End Count_Number;

2)用for实现:for i in 1..l00 loop ....end loop;

Create Or Replace Procedure Count_Number Is
  i Number(10);
  t Number(10);
Begin
 --- i := 1;
  t := 0;
  for i in 1..100 loop
  t:=t+i;
  Dbms_Output.Put_Line('it''s :' || t);
  end loop;
   ---Dbms_Output.Put_Line('it''s :' || t);
End Count_Number;


for循环:
[PL/SQL] 用For Loop 替代Cursor
http://www.itwenzhai.com/data/2006/0523/article_9377.htm
http://blog.youkuaiyun.com/heyday/archive/2005/07/27/435804.aspx

CURSOR FOR Loop
    FOR employee_rec in c1 ---employee_rec直接用,不用提前定义
    LOOP
    total_val := total_val + employee_rec.monthly_income;
    END LOOP;
    当使用CURSOR FOR Loop时,不用我手工open cursor close cursor
应用:
begin
        FOR emm IN ( SELECT ro_site, ns_site, product_line, wh_type
                     FROM eis_hq_invhl_mail_data
                    WHERE report_type = 'Detailed' )
       LOOP
       DBMS_OUTPUT.put_line( emm.product_line );
           eis_hq_invhl_pkg.make_mail_detailed_data
                                       ( p_ro_site=>           emm.ro_site,
                                         p_ns_site=>           emm.ns_site,
                                         p_product_line=>      emm.product_line,
                                         p_wh_type=>           emm.wh_type,
                                         p_current_day=>       to_date('2005-11-07','yyyy-MM-dd') );
       END LOOP;
end;
存储过程DIY2----游标与循环
http://blog.youkuaiyun.com/brave1/archive/2005/06/08/390160.aspx

while循环:
WHILE( i <= CEIL( LENGTH( p_clob_data ) / 4000))
 

   LOOP
       v_clob_data := TRIM( SUBSTR( p_clob_data,
                                    i * 4000,
                                    4000 ));
       DBMS_OUTPUT.put_line( SUBSTR( v_clob_data,
                                     0,
                                     255 ));
      
       i := i + 1;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值