繁体   English   中英

如何正确处理此pl / sql for循环中的异常?

[英]How do I handle the exception in this pl/sql for loop correctly?

首先,我是PL / SQL的新手,所以我可能会错过一些琐碎的事情。

这是我在运行时遇到问题的一小段代码-

FOR indx IN 1 .. arr.COUNT
    LOOP
        SELECT COUNT(*), ca.cities
        INTO tmp_count, affected_cities
        FROM PDB.utilities ca
        WHERE (ca.app_city_id           = cityid
        AND ca.app_plumbing_id = arr(indx))
        AND( BITAND(options1,2)        = 2
            OR BITAND(options1,1)          = 1)
        GROUP BY ca.cities;

        IF tmp_count                  >=0 THEN
             -- We have an affected app so collect metrics
            IF plumbings(indx_mv)  ='0Ci30000000GsBN' THEN
                count_wrigley:= count_wrigley+tmp_count;
            END IF;
            counter:= counter+tmp_count; --overall count. 
            tmp_count:=0;
            affected_cities:=null;
        END IF;

        EXCEPTION -- error thrown here !
            WHEN NO_DATA_FOUND THEN
                CONTINUE;
        END;  
    END LOOP; -- Error thrown here too. 

这是我的错误跟踪-

Error report:
ORA-06550: line 64, column 13:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:

   ( begin case declare end exit for goto if loop mod null
   pragma raise return select update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   continue close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge pipe purge
ORA-06550: line 68, column 11:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:

   ;
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

值得注意的是,该块仅在异常处理后失败,否则将成功编译。 所以我猜我在那儿做错了吗?

任何帮助将不胜感激! 谢谢

EXCEPTION与BEGIN ... END块对齐。 循环中没有BEGIN,因此也不应该例外。

看起来异常的目的是抑制循环内的NO_DATA_FOUND错误。 因此,要解决此错误,您还需要在循环中放置一个BEGIN / END块。 (嗯,您只有一个END而没有BEGIN-您的代码会在EXCEPTION块中抛出)。

FOR indx IN 1 .. arr.COUNT
LOOP
    BEGIN 
        SELECT COUNT(*), ca.cities
        INTO tmp_count, affected_cities
        FROM PDB.utilities ca
        ....
    EXCEPTION 
          WHEN NO_DATA_FOUND THEN
             CONTINUE;
    END;  
END LOOP; 

暂无
暂无

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

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