PL/SQL文法

來源:互聯網
上載者:User

標籤:基本文法   結構   pl/sql   

1.分支結構

    PL/SQL中,使用if關鍵字作為分之結構的程式起始段。

    總體有以下幾種分支結構:

    1)if condition then statement end if;

    2)if condition then statement 

        else then statement

        end if;

    3)if condition then statement

        elsif condition then statement

        ...

        else then statement

        end if;

    分別對應於其它語言中的if(){},if(){}else(){},if(){}else if(){}...else{}結構

    此外,PL/SQL還提供case關鍵字作為分支結構的標識段。

    case的用法有以下幾種:

    1)對某個欄位進行單值匹配

        case column1

            when value1 then statement

            when value2 then statement

            ...

        end case;

    這種結構類似於其它語言中的switch結構。

    2)使用case..when結構來表達if..else條件文法

        case 

            when condition then statement

            when condition then statement

           ...

        end case;

2.迴圈結構

    PL/SQL中,具體的迴圈結構有以下幾種:

    1)for

    for i in rs.first..rs.last loop

    end loop;

    這種結構在遍曆遊標指向的集合時,顯得簡潔而高效,該結構可實現自動管理遊標:包括遊標的開啟、關閉和自動fetch。

    樣本: 

    declare  

        cursor c_emp is 

               select * from emp;

        emp_record emp%rowtype;

    begin

        for emp_record in c_emp

            loop

                 dbms_output.put_line(emp_record.empno);

            end loop;

    end;

     這段PL/SQL程式並未開啟遊標,也沒有使用fetch語句,更沒有關閉遊標,但是使用for..in結構可以自動隱式完成這些工作,因此簡潔高效,且不易出錯。

  2)loop when

    loop

    exit when (statement)

    end loop;

     3)loop while

    while (statement)

    loop

    end loop;

      4)goto

    loop

    if (statement) then goto label_name

    end if;

    end loop;

    <<label_name>>

    在PL/SQL中,可以使用<<label_name>>的形式定義一個標籤,使用goto關鍵字即可讓程式在執行時跳轉至該標籤,從而結束迴圈體。



本文出自 “notDebug” 部落格,轉載請與作者聯絡!

PL/SQL文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.