loop退出循环体:
1. goto next; <<next>> null;
2. exit when boolean表达式;
示例:
CREATE OR REPLACE PROCEDURE pro_end_loop(i_num NUMBER) AS
v_ind NUMBER DEFAULT 1;
v_num NUMBER;
BEGIN
v_num := i_num;
dbms_output.put_line('i_num='||i_num);
-- 1. 使用exit when boolean表达式 退出循环
LOOP
--v_ind值为5时, 退出循环体
EXIT WHEN v_ind =5;
v_ind := v_ind + 1;
dbms_output.put_line('v_ind='||v_ind||', i_num='||i_num);
END LOOP;
-- 2.使用goto next
LOOP
v_ind := v_ind + 1;
IF v_ind =10 THEN
-- v_ind值为10时, 流程跳转到<<next>>的位置
GOTO NEXT;
END IF;
END LOOP;
dbms_output.put_line('leval loop');
--后面的null必须, next后面需要跟一个语句
<<NEXT>> NULL;
dbms_output.put_line('leval loop to next');
END pro_end_loop;
loop结束当前循环,开始下一轮循环
1. continue (测试数据库为12c)
示例:
CREATE OR REPLACE PROCEDURE pro_end_loop(i_num NUMBER) AS
v_ind NUMBER DEFAULT 1;
v_num NUMBER;
BEGIN
v_num := i_num;
dbms_output.put_line('i_num='||i_num);
dbms_output.put_line('使用continue中断当前循环,开始下一个循环');
LOOP
dbms_output.put_line('into loop continue before');
v_ind := v_ind + 1;
IF v_ind =3 THEN
dbms_output.put_line('continue block');
-- v_ind的值为3时, 结束当前循环, 进入下一次循环
CONTINUE;
END IF;
dbms_output.put_line('into loop continue after');
END LOOP;
END pro_end_loop;