【问题标题】:Stop PL/SQL code after exception (PL/SQL, ORACLE)异常后停止 PL/SQL 代码(PL/SQL、ORACLE)
【发布时间】:2018-01-12 11:08:41
【问题描述】:

我有检查程序:

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;

如果引发异常,我还希望在消息旁边停止/中断所有其他 PL/SQL 代码(将不执行过程过程 3 和 4)。

喜欢:

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

我该怎么做?

【问题讨论】:

  • 将它们包装在另一个过程中,将异常响应作为变量从过程中更改出来,根据变量在外部过程中引发异常
  • 你能告诉我怎么做吗?

标签: oracle plsql exception-handling oracle12c


【解决方案1】:

您可以从checkVariables 过程中重新引发异常。在带有EXCEPTION 块的BEGIN..END 中运行所有程序

...
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

【讨论】:

  • 谢谢,我还添加了 ROLLBACK: when it's failed and when it success
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多