Oracle中的for和while迴圈

來源:互聯網
上載者:User

有兩種方式可以實現條件迴圈
一、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
http://www.itwenzhai.com/data/2006/0523/article_9377.htm 
http://blog.csdn.net/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.csdn.net/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;
    END LOOP;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.