请教各位高手 oracle 存储过程 如何获得 捕获异常的内容

rt

第1个回答  推荐于2017-11-28
--通过sqlcode , sqlerrm 这两个内置变量来查看,例如:

DECLARE
--声明异常
some_kinds_of_err EXCEPTION; -- Exception to indicate an error condition

v_ErrorCode NUMBER; -- Variable to hold the error message code
v_ErrorText VARCHAR2(200); -- Variable to hold the error message text

BEGIN
--...
--抛出异常
IF ( ... ) THEN --(括号内填抛出异常的条件)
RAISE some_kinds_of_err;
END IF;
--...
EXCEPTION
--捕捉异常
WHEN some_kinds_of_err THEN
/* do something to Handler the errors */
null;
--捕捉其他异常,并获得 捕获异常的内容
WHEN OTHERS THEN
v_ErrorCode := SQLCODE;
v_ErrorText := SUBSTR(SQLERRM, 1, 200);
-- Note the use of SUBSTR here.

dbms_output.put_line(v_ErrorCode || '::'||v_ErrorText);
END;
/

/**
sqlcode 就是错误代码
sqlerrm 就是sql错误信息。注意用substr来截取,否则输出很难看。

**/本回答被提问者采纳
第2个回答  2010-06-28
begin
plsql语句串;
exception when others
then
dbms_output.put_line(sqlcode||' '|| SQLERRM);

end;
相似回答