其中,LOOP 和 END LOOP 之间的语句中,至少要有一个语句包含 EXIT 语句;否则 LOOP 语句会一直重复永不停止。

    示例:使用基本的 LOOP 和 EXIT WHEN 语句

    1. CREATE OR REPLACE PROCEDURE sp_test_loop_exit_when
    2. AS
    3. i_counter number := 10;
    4. BEGIN
    5. LOOP
    6. dbms_output.put_line('In the loop i_counter is ' || to_number(i_counter) );
    7. i_counter := i_counter - 1;
    8. EXIT WHEN i_counter <= 0;
    9. END LOOP;
    10. dbms_output.put_line('Out of the loop i_counter is ' || to_number(i_counter) );
    11. EXCEPTION
    12. WHEN OTHERS THEN
    13. NULL;
    14. END;
    15. /
    16. delimiter ;
    17. obclient>
    18. obclient> set serveroutput on;
    19. Query OK, 0 rows affected (0.00 sec)
    20. obclient> call sp_test_loop_exit_when();
    21. In the loop i_counter is 10
    22. In the loop i_counter is 9
    23. In the loop i_counter is 8
    24. In the loop i_counter is 7
    25. In the loop i_counter is 6
    26. In the loop i_counter is 5
    27. In the loop i_counter is 4
    28. In the loop i_counter is 3
    29. In the loop i_counter is 2
    30. In the loop i_counter is 1
    31. obclient>