zzw原創_oracle循環中的異常捕捉_捕捉異常后並繼續循環


例子如下

set serveroutput on;

declare
   V_SQL VARCHAR2(255);
   errorCode number; --異常編碼  
   errorMsg varchar2(1000); --異常信息
   out_return varchar2(1000);
    flag varchar2(10);
CURSOR TP IS
  SELECT TABLE_NAME,CONSTRAINT_NAME FROM USER_CONSTRAINTS  WHERE CONSTRAINT_TYPE = 'P';
begin
    FOR E IN TP LOOP
    BEGIN
    V_SQL := 'ALTER TABLE ' || E.TABLE_NAME || ' ' || 'DISABLE' ||'  '|| 'CONSTRAINT ' || E.CONSTRAINT_NAME;
    DBMS_OUTPUT.PUT_LINE(V_SQL);
    EXECUTE IMMEDIATE (V_SQL);
     EXCEPTION
     when others then
         errorCode := SQLCODE;    
          errorMsg := SUBSTR(SQLERRM, 1, 200);   
              flag := 'false';  
        out_return := 'flag=' || flag || ',errorCode=' || errorCode || ',errorMsg=' || errorMsg;
        dbms_output.put_line(out_return);
      null;
    END;
   END LOOP;
end;

 

注意問題

1、捕捉異常后繼續下一次循環需用

     EXCEPTION
     when others then  null;

    這樣的結構。

2、在for ...LOOP   ENDLOOP 循環中捕捉異常,必須用begin   end 包起來,否則會報錯。在PLSQL中報

    PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following
    Error: PLS-00103: Encountered the symbol "END" when expecting one of the following

  正確


    FOR E IN TP LOOP
    begin
      ......................
     EXCEPTION
     when others then  null;

   end;

   endloop;

 

錯誤用法:

FOR E IN TP LOOP
      ......................
     EXCEPTION
     when others then  null;

   endloop;

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM