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

Stop PL/SQL code after exception (PL/SQL, ORACLE)

我有检查程序:

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

我该怎么做?

您可以从 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