其中,LOOP 和 END LOOP 之间的语句中,至少要有一个语句包含 EXIT 语句;否则 LOOP 语句会一直重复永不停止。
示例:使用基本的 LOOP 和 EXIT WHEN 语句
CREATE OR REPLACE PROCEDURE sp_test_loop_exit_when
AS
i_counter number := 10;
BEGIN
LOOP
dbms_output.put_line('In the loop i_counter is ' || to_number(i_counter) );
i_counter := i_counter - 1;
EXIT WHEN i_counter <= 0;
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_loop_exit_when();
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
obclient>