WHILE LOOP 语句会在判断条件结果为 TRUE 的时候,重复执行一序列语句。如果条件结果为 FALSE 了,则退出循环。语法格式如下:
WHILE condition LOOP
statement [, statement ]...
END LOOP;
注意
如果 LOOP 和 END LOOP 之间的语句永远不改变,WHILE 后的条件结果为 FALSE ,则 WHILE LOOP 循环会一直重复不停止,直到强行中断子程序会话。
示例:使用 WHILE LOOP 语句
delimiter /
CREATE OR REPLACE PROCEDURE sp_test_while_loop
AS
i_counter number := 10;
BEGIN
WHILE(i_counter > 0) LOOP
dbms_output.put_line('In the loop i_counter is ' || to_number(i_counter) );
i_counter := i_counter - 1;
END LOOP;
dbms_output.put_line('Out of the loop i_counter is ' || to_number(i_counter) );
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
/
delimiter ;
obclient>
obclient> set serveroutput on;
Query OK, 0 rows affected (0.00 sec)
obclient> call sp_test_while_loop();
Query OK, 0 rows affected (0.04 sec)
In the loop i_counter is 10
In the loop i_counter is 9
In the loop i_counter is 8
In the loop i_counter is 7
In the loop i_counter is 6
In the loop i_counter is 5
In the loop i_counter is 4
In the loop i_counter is 3
In the loop i_counter is 2
In the loop i_counter is 1
Out of the loop i_counter is 0
obclient>