Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果循环PL SQL中满足条件,如何退出过程_Sql_Oracle_Loops_Plsql_Procedure - Fatal编程技术网

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

如果循环PL SQL中满足条件,如何退出过程,sql,oracle,loops,plsql,procedure,Sql,Oracle,Loops,Plsql,Procedure,假设我有一个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 k

假设我有一个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”放在循环之外,那么我认为无论循环的结果如何,当循环结束时,它都会退出整个过程。

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

在使用之后,唯一推荐的方法是使用while循环,并在开始部分中指定所需的条件

以下是上述链接的内容和摘录:

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;
代码清单5:一个带有一个出口的WHILE循环
程序显示\u多年(
开始年份请输入整数
,年末(整数)
是
l\u当前\u年PLS\u整数:=开始\u年\u in;
开始
WHILE(l_当前_第0年)
环
显示本年度(本年度)的销售总额;
l_当前_年:=l_当前_年+1;
端环;
结束显示数年;

定义一个异常,当您想要退出时,根据需要引发并处理异常

    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;

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