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

New*_*ser 1 oracle plsql

首先,我是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. 
Run Code Online (Sandbox Code Playgroud)

这是我的错误跟踪 -

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:
Run Code Online (Sandbox Code Playgroud)

值得注意的是,该块仅在异常处理时失败,否则成功编译.所以我的猜测是我在那里做错了什么?

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

APC*_*APC 5

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; 
Run Code Online (Sandbox Code Playgroud)