2011-12-19 17 views
7

カーソルを実行しようとしていて、何らかの例外があってもループを完了させようとしています。plsql /カーソルが例外を処理して実行フローに戻る

私がやろうとしているのは、すべての例外を "キャッチ"して、何もしないでログに記録してから、フローに戻ることです。コードは次のようになります。

FOR line IN my_cursor 
LOOP 
begin 

    if<condition> then 
     GOTO pass; 
    else  
    <<do_something>> 
    exception 
     when others then 
     sys.dbms_output.put_line('say something');  
    end if; 

    <<pass>> null; 
end 
END LOOP; 

スクリプトはコンパイルされません。 例外には多分構文上のエラーがありますが、私はセマンティクスもよく認識していません。例外を処理した後に実行フローに戻ることができるかどうかはわかりません。

p.s:DBは10gで、CONTINUEはありません。したがって、GOTOを使用します。

+2

、 '。 –

+0

thnx。それは問題のタイプミスでした。 – codeObserver

+0

提案された回答の1つが適格と認められるかどうかを確認できますか? TIA – lkuty

答えて

16

ループ内で実行したいコードを独自のブロックに入れて、そのループを使用して、ループ反復中の問題を処理することができます。その繰り返しのために例外が処理された後、次のループの繰り返しが始まります

例えば

for line in my_cursor 
loop 
    begin  
     <<do_something>> 
    exception 
     <<do_exception_processing>>   
    end; 
end loop; 

は、以下の例では、I型のローカル変数を宣言している、これをさらに説明するために例外。私は1から10までの数字をループしています.2回目のループの反復でif文がtrueで、処理が例外ハンドラに渡されます。例外が処理されると、ループの次の反復が開始されます。 `then`と`エンドif`を必要とif`また

begin 

    for i in 1 .. 10 
    loop 

     declare 

     my_exception exception; 

     begin 

     if i = 2 
     then 

      -- if you need to do some processing then you would enter it 
      -- here and then when you want to enter the exception section 
      -- you would add the line below 

      raise my_exception; 

     end if; 

     exception 
     when my_exception then 
      dbms_output.put_line('in exception section'); 

     end; 

    end loop; 

end; 
+1

Thnx Ian。私はそれを試み、質問で更新しました。問題はまだあります.LOOPには "begin"と "end"の必要性がありますか? – codeObserver

+2

はい、例外セクションを使用するにはbegin ... endセクションが必要です –

+0

私の答えは –

7
FOR line IN my_cursor 
LOOP 
    if not some_condition then 
    begin 
     do_something; 
    exception  
     when others then log_my_error(); -- this should be something that uses 
             -- an autonomous transaction 
    end; 
    end if; 
END LOOP; 
0
BEGIN 
FOR Line in My_Cursor LOOP 
    IF condition THEN 
BEGIN 
    do something... 
END; 
    ELSE 
BEGIN 
    do something... 
END; 
    END IF; 
END LOOP; 
EXCEPTION 
WHEN OTHERS THEN 
DBMS_OUTPUT.PUT_LINE('say something'); 
END;