2011-06-06 62 views
5

我有一個存儲過程異常處理/ SQL

create or replace procedure Trial 
    is 
Begin 
---Block A-- 
EXCEPTION 
when others then 
insert into error_log values('error'); 
--Block A ends---- 
--Block B ---- 
----Block B ends--- 
end; 

我想在方框B的代碼中的所有條件,即執行如果在塊甲異常升高或not.With以上代碼B座執行只有在引發異常的情況下。 如何做到這一點。 請幫忙。

回答

6

您可以創建嵌套塊:

create or replace procedure Trial 
    is 
Begin 
    begin 
    ---Block A-- 
    EXCEPTION 
    when others then 
     insert into error_log values('error'); 
    end; 
    begin 
    --Block B ---- 
    end; 
end; 
+1

你並不真的需要周圍塊B的'begin' /'end',但他們可能在這種情況下更加明確。 – 2011-06-06 13:16:21

2

請注意,這是一個常見的反模式捕獲所有異常不提高他們。您可能還想考慮一個自治事務,以便在回滾之後保留錯誤日誌。

所以你可能是更好的東西掉這樣的:

create or replace procedure Trial 
is 

    procedure finally is 
    begin 
    --Block B ---- 
    end; 

    procedure logerr (msg varchar2) is 
    PRAGMA AUTONOMOUS_TRANSACTION; 
    begin 
    insert into error_log values(msg); 
    commit; 
    end; 

Begin 
    begin 
    ---Block A-- 
    EXCEPTION 
    when others then 
     logerr(SQLERRM); 
     finally; 
     raise; 
    end; 

    finally; 
end;