Oracle中的for和while循环

Oracle中的for和while循环  
 

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

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

二、while 条件 loop       end  loop;

loop的使用方式:

一、X := 100;

  LOOP

    X := X + 10;

    IF X > 1000 THEN

      EXIT;

    END IF END LOOP;

    Y := X;

二、 X := 100;

    LOOP

      X := X + 10;

      EXIT WHEN X > 1000;

      X := X + 10;

    END LOOP;

    Y := X;

还有一个GOTO的使用,在后面的过程中会有的,用来产生错误日志。

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

create or replace procedure count_number is

  /*

  功能描述:计算计算出1到100的和

  t的值为和

  */

  i number(10);

  t number(10);

begin

  /*

    i := 1;

    t := 0;

  --用while+loop实现

    /*begin

      while i <= 100 loop

        t := t + i;

        i := i + 1;

      end loop;

    end;

    */

  i := 1;

  t := 0;

  --用for实现

  begin

    for i in 1 .. 100 loop

      t := t + i;

   

    end loop;

  end;

end count_number;

for循环:

[PL/SQL] 用For Loop 替代Cursor


 

 

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----游标与循环

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;

    END LOOP;

posted on 2010-11-10 16:07  darren.yang  阅读(58230)  评论(0编辑  收藏  举报

导航