如果在循环中满足条件,如何退出程序 PL SQL
how to exit the procedure if condition met in a loop PL SQL
假设我有一个 for 循环
for i in array.first .. array.last loop
boolean := c(i) > d(i);
if boolean --is true then
exit the loop immediately and also exit the entire procedure
else if the boolean is never true til the end of the loop, exit the loop
and keep running other scripts in this procedure.
我知道 'EXIT' 关键字需要在循环内才能在满足条件时退出循环。 'RETURN' 需要在循环之外,才能退出程序。
但是如果我把'RETURN'放在循环外面,那么我想不管循环结果是什么,循环结束都会退出整个程序?
简单的循环。之所以称为简单是有原因的:它简单地以 LOOP 关键字开始,以 END LOOP 语句结束。如果您在循环体内执行 EXIT、EXIT WHEN 或 RETURN(或者引发异常),则循环将终止。
在 Tamás Kecskeméti's link 之后,唯一推荐的方法是使用 while 循环,并在开头本身指定所需的条件。
以下是上面的摘录 link :
Code Listing 5: A WHILE loop with one exit
PROCEDURE display_multiple_years (
start_year_in IN PLS_INTEGER
, end_year_in IN PLS_INTEGER)
IS
l_current_year PLS_INTEGER := start_year_in;
BEGIN
WHILE ( l_current_year <= end_year_in
AND total_sales_for_year (l_current_year) > 0)
LOOP
display_total_sales_for_year (l_current_year);
l_current_year := l_current_year + 1;
END LOOP;
END display_multiple_years;
定义一个异常,当你想退出时引发并处理异常
create procedure exit_loop_example as
exit_loop_exception exception;
begin
/* previous code block */
begin
for i in 1..20 loop
raise exit_loop_exception;
end loop;
exception when
exit_loop_exception then
/* handle exit loop*/
null;
end;
/* next code block */
end;
如果你想对其进行说教,你应该使用 EXIT 退出循环,然后使用 RETURN 退出程序(重复任何必要的测试),从而遵循结构化编程规则 "A procedure should only have a single entrance and a single exit"。在实践中,99.999% 的程序员只会在循环体内编写 RETURN 代码,因为 A) 它更清楚发生了什么(你不只是退出循环,你正在从过程中返回), B) 它更短。随心所欲。祝你好运。
假设我有一个 for 循环
for i in array.first .. array.last loop
boolean := c(i) > d(i);
if boolean --is true then
exit the loop immediately and also exit the entire procedure
else if the boolean is never true til the end of the loop, exit the loop
and keep running other scripts in this procedure.
我知道 'EXIT' 关键字需要在循环内才能在满足条件时退出循环。 'RETURN' 需要在循环之外,才能退出程序。
但是如果我把'RETURN'放在循环外面,那么我想不管循环结果是什么,循环结束都会退出整个程序?
简单的循环。之所以称为简单是有原因的:它简单地以 LOOP 关键字开始,以 END LOOP 语句结束。如果您在循环体内执行 EXIT、EXIT WHEN 或 RETURN(或者引发异常),则循环将终止。
在 Tamás Kecskeméti's link 之后,唯一推荐的方法是使用 while 循环,并在开头本身指定所需的条件。
以下是上面的摘录 link :
Code Listing 5: A WHILE loop with one exit
PROCEDURE display_multiple_years (
start_year_in IN PLS_INTEGER
, end_year_in IN PLS_INTEGER)
IS
l_current_year PLS_INTEGER := start_year_in;
BEGIN
WHILE ( l_current_year <= end_year_in
AND total_sales_for_year (l_current_year) > 0)
LOOP
display_total_sales_for_year (l_current_year);
l_current_year := l_current_year + 1;
END LOOP;
END display_multiple_years;
定义一个异常,当你想退出时引发并处理异常
create procedure exit_loop_example as
exit_loop_exception exception;
begin
/* previous code block */
begin
for i in 1..20 loop
raise exit_loop_exception;
end loop;
exception when
exit_loop_exception then
/* handle exit loop*/
null;
end;
/* next code block */
end;
如果你想对其进行说教,你应该使用 EXIT 退出循环,然后使用 RETURN 退出程序(重复任何必要的测试),从而遵循结构化编程规则 "A procedure should only have a single entrance and a single exit"。在实践中,99.999% 的程序员只会在循环体内编写 RETURN 代码,因为 A) 它更清楚发生了什么(你不只是退出循环,你正在从过程中返回), B) 它更短。随心所欲。祝你好运。