WHILE LOOP 语句会在判断条件结果为 TRUE 的时候,重复执行一序列语句。如果条件结果为 FALSE 了,则退出循环。语法格式如下:

    1. WHILE condition LOOP
    2. statement [, statement ]...
    3. END LOOP;

    注意

    如果 LOOP 和 END LOOP 之间的语句永远不改变,WHILE 后的条件结果为 FALSE ,则 WHILE LOOP 循环会一直重复不停止,直到强行中断子程序会话。

    示例:使用 WHILE LOOP 语句

    1. delimiter /
    2. CREATE OR REPLACE PROCEDURE sp_test_while_loop
    3. AS
    4. i_counter number := 10;
    5. BEGIN
    6. WHILE(i_counter > 0) LOOP
    7. dbms_output.put_line('In the loop i_counter is ' || to_number(i_counter) );
    8. i_counter := i_counter - 1;
    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_while_loop();
    21. Query OK, 0 rows affected (0.04 sec)
    22. In the loop i_counter is 10
    23. In the loop i_counter is 9
    24. In the loop i_counter is 8
    25. In the loop i_counter is 7
    26. In the loop i_counter is 6
    27. In the loop i_counter is 5
    28. In the loop i_counter is 4
    29. In the loop i_counter is 3
    30. In the loop i_counter is 2
    31. In the loop i_counter is 1
    32. Out of the loop i_counter is 0
    33. obclient>