PLSQL Language Referenc-FOR迴圈-FOR迴圈中的EXIT WHEN和CONTINUE WHEN

LuiseDalian發表於2014-03-11

FOR迴圈中的EXIT WHENCONTINUE WHEN

DECLARE

    v_employees employees%ROWTYPE;

    CURSOR c1 is SELECT * FROM employees;

BEGIN

    OPEN c1;

    -- 獲取整個行

    FOR i IN 1..10 LOOP

        FETCH c1 INTO v_employees;

        EXIT WHEN c1%NOTFOUND;

        -- 在此處理資料

    END LOOP;

    CLOSE c1;

END;

 

DECLARE

    v_employees employees%ROWTYPE;

    CURSOR c1 is SELECT * FROM employees;

BEGIN

    OPEN c1;

 

    -- 獲取整行

    <<outer_loop>>

    FOR i IN 1..10 LOOP

        -- 在此處理資料

        FOR j IN 1..10 LOOP

            FETCH c1 INTO v_employees;

            --直接退出外部迴圈

            EXIT outer_loop WHEN c1%NOTFOUND;

        -- 在此處理資料

        END LOOP;

    END LOOP outer_loop;

  CLOSE c1;

END;

 


 

--不管表中有多少條記錄,迴圈都做100

DECLARE

    v_employees employees%ROWTYPE;

    CURSOR c1 is SELECT * FROM employees;

BEGIN

    OPEN c1;

 

    <<outer_loop>>

    FOR i IN 1..10 LOOP   

        DBMS_OUTPUT.PUT_LINE('外部迴圈 i = ' || i);

        FOR j IN 1..10 LOOP

            DBMS_OUTPUT.PUT_LINE('內部迴圈 j = ' || j);

            FETCH c1 INTO v_employees;

            CONTINUE outer_loop WHEN c1%NOTFOUND;

        END LOOP;

    END LOOP outer_loop;

    CLOSE c1;

END;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17013648/viewspace-1107112/,如需轉載,請註明出處,否則將追究法律責任。

相關文章