2017-05-11 45 views
1

首先,我是PL/SQL的相對新手,所以我可能會錯過一些微不足道的東西。如何正確處理這個pl/sql for循環中的異常?

這裏的代碼片段,我有問題與運行 -

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: 

值得一提的是,該塊失敗只能用異常處理,否則成功編譯。所以我的猜測是我在那裏做錯了什麼?

任何幫助將不勝感激!謝謝

回答

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;