oracle 如果在循环 PL SQL 中满足条件,如何退出过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41077138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:20:54  来源:igfitidea点击:

how to exit the procedure if condition met in a loop PL SQL

sqloracleloopsplsqlprocedure

提问by dozel

Let's say I have a for loop

假设我有一个 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.

I know the 'EXIT' keyword needs to be inside of the loop in order to exit the loop if condition is met. And 'RETURN' needs to be outside of the loop, to exit the procedure.

我知道 'EXIT' 关键字需要在循环内才能在满足条件时退出循环。并且 'RETURN' 需要在循环之外,以退出该过程。

But if I put 'RETURN' outside of the loop, then I think no matter what the result is from the loop, it will exit the entire procedure when the loop ends?

但是如果我把'RETURN'放在循环之外,那么我认为无论循环的结果是什么,循环结束时它都会退出整个过程?

回答by Bob Jarvis - Reinstate Monica

If you want to be didactic about it, you should use an EXIT to get out of the loop, then use a RETURN to exit from the procedure (duplicating any necessary tests), thus following the structured programming rule that "A procedure should only have a single entrance and a single exit". In practice 99.999% of programmers would just code the RETURN inside the body of the loop because A) it's clearer as to what's going on (you're not just getting out of the loop, you're returning from the procedure), and B) it's shorter. Do as you will. Best of luck.

如果你想说教,你应该使用 EXIT 退出循环,然后使用 RETURN 退出过程(复制任何必要的测试),从而遵循结构化编程规则“一个过程应该只有一个入口和一个出口”。在实践中,99.999% 的程序员只会在循环体内部编写 RETURN 代码,因为 A)它更清楚发生了什么(你不仅仅是退出循环,你是从过程中返回),并且 B )它更短。随心所欲。祝你好运。

回答by Tamás Kecskeméti

The simple loop. It's called simple for a reason: it starts simply with the LOOP keyword and ends with the END LOOP statement. The loop will terminate if you execute an EXIT, EXIT WHEN, or RETURN within the body of the loop (or if an exception is raised).

简单的循环。它被称为简单是有原因的:它以 LOOP 关键字开始,以 END LOOP 语句结束。如果您在循环体中执行 EXIT、EXIT WHEN 或 RETURN(或引发异常),则循环将终止。

See Oracle Magazine

见甲骨文杂志

回答by Ravindra HV

Following through with Tamás Kecskeméti's link, the only recommended way is to use a while loop with the desired condition specified in the beginning itself.

通过Tamás Kecskeméti链接,唯一推荐的方法是使用 while 循环,并在开头指定所需的条件。

Below is and excerpt from the above 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;

回答by fabribara

define an exception, and when ever you want to exit raise and handle the exception as

定义一个异常,当你想退出 raise 并处理异常时

    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;