异常后停止PL / SQL代码(PL / SQL,ORACLE)

时间:2018-01-12 11:08:41

标签: oracle plsql exception-handling oracle12c

我有检查程序:

PROCEDURE checkVariables                               
IS
   r_Var1    exception;
   r_Var2    exception;
BEGIN

 If g_Name is null then     
        RAISE r_Var1;
 End if;
 If g_Prefix is null then     
        RAISE r_Var2;
 End if;

 DBMS_OUTPUT.PUT_LINE('Variables Set Up');

 EXCEPTION
        When r_Var1 then
           DBMS_OUTPUT.PUT_LINE('Missing g_Name');               
        When r_Var2 then
           DBMS_OUTPUT.PUT_LINE('Missing g_Prefix');            
END;

如果引发异常,我希望旁边的消息也是STOP / BREAK所有其他PL / SQL代码(程序过程3和4将不会被执行)。

像:

execute procedure1
execute procedure2
execute checkVariables --raised exception, STOP/BREAK next code                               
execute procedure3
execute procedure4

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以从checkVariables程序中重新引发异常。使用BEGIN..END

EXCEPTION内运行所有程序
...
EXCEPTION
  WHEN r_var1 THEN
             DBMS_OUTPUT.PUT_LINE('Missing g_Name');
             RAISE; 

  WHEN r_var2 THEN
             DBMS_OUTPUT.PUT_LINE('Missing g_Prefix');
             RAISE;
END;
... 
BEGIN

  procedure1;
  procedure2;
  checkVariables; --raised exception, STOP/BREAK next code                               
  procedure3;
  procedure4; 

EXCEPTION

WHEN  OTHERS THEN 

  DBMS_OUTPUT.PUT_LINE('EXCEPTION  OCCURED');
  DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE()); --Gives additional information

END;
/

输出会是这样的。

PROC1
PROC2
Missing g_Name
EXCEPTION  OCCURED
ORA-06512: at "HR.CHECKVARIABLES", line 21
ORA-06512: at "HR.CHECKVARIABLES", line 10
ORA-06512: at line 5