如何在 ORACLE PL/SQL 创建或替换存储过程中捕获和返回(或打印)异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12327643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch and return (or print) an exception in an ORACLE PL/SQL create or replace stored procedure
提问by n00b
I have a PL/SQL stored procedure inside which I want to run several updates, then capture any exceptions and return them in an out parameter. For simplicitys sake in the code block below I am simply outputting the exception code and error message to the console. However, the code block below does not work (I am experiencing a "found / expecting" syntax error)
我有一个 PL/SQL 存储过程,我想在其中运行多个更新,然后捕获任何异常并在 out 参数中返回它们。为了简单起见,在下面的代码块中,我只是将异常代码和错误消息输出到控制台。但是,下面的代码块不起作用(我遇到了“发现/期望”语法错误)
CREATE OR REPLACE PROCEDURE DBP.TESTING_SP AS
DECLARE
v_code NUMBER;
v_errm VARCHAR2(64);
BEGIN
UPDATE PS_NE_PHONE_TBL SET NE_PHONE_TYPE = 'TEST' WHERE NEMPLID_TBL = 'N14924';
EXCEPTION
WHEN OTHERS THEN
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1, 64);
DBMS_OUTPUT.PUT_LINE (v_code || ' ' || v_errm);
END TESTING_SP;
/
What is the correct syntax for what I am trying to do?
我正在尝试做的正确语法是什么?
I read on one forum
我在一个论坛上读到
"When using the Create or Replace syntax, you can not use Declare. Declare is only for anonymous blocks that are not named. So either remove line 1 and create an anonymous block, or remove line 2 and create a named procedure."
“使用 Create 或 Replace 语法时,您不能使用 Declare。Declare 仅适用于未命名的匿名块。因此,要么删除第 1 行并创建一个匿名块,要么删除第 2 行并创建一个命名过程。”
but I'm not sure how to create an anonymous block or create a named procedure to accomplish what I want to do (if that, indeed, is the 'solution'). Could somebody lend a hand?
但我不确定如何创建匿名块或创建命名过程来完成我想做的事情(如果那确实是“解决方案”)。有人可以帮忙吗?
采纳答案by DCookie
Just remove the DECLARE statement.
只需删除 DECLARE 语句。