简体   繁体   English

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

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

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. 我知道'EXIT'关键字需要在循环内才能在满足条件的情况下退出循环。 And 'RETURN' needs to be outside of the loop, to exit the procedure. 并且“ 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”放到循环之外,那么我认为无论循环返回什么结果,当循环结束时它将退出整个过程?

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". 如果要对此进行教did,则应使用EXIT退出循环,然后使用RETURN退出过程(重复任何必要的测试),因此遵循结构化的编程规则,即“过程仅应具有一个入口和一个出口”。 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. 在实践中,有99.999%的程序员会只在循环体内编写RETURN,因为A)清楚发生了什么(您不只是退出循环,还从过程中返回),以及B )较短。 Do as you will. 尽你所能。 Best of luck. 祝你好运。

The simple loop. 简单循环。 It's called simple for a reason: it starts simply with the LOOP keyword and ends with the END LOOP statement. 之所以将其称为简单是因为:它以LOOP关键字开始,以END LOOP语句结束。 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). 如果在循环体内执行EXIT,EXIT WHEN或RETURN(或引发异常),循环将终止。

See Oracle Magazine 参见《 Oracle杂志》

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ásKecskemé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;

define an exception, and when ever you want to exit raise and handle the exception as 定义一个异常,并且无论何时要退出引发并以

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM