oracle存储过程错误跟踪

时间:2022-05-19 07:57:25

1.首先创建用于保存错误信息的表

1
2
3
4
5
6
7
8
CREATE TABLE TBL_PROC_ERRMSG
(
  BIZ_CODE VARCHAR2(50),
  ERR_LINE VARCHAR2(10),
  ERR_CODE VARCHAR2(10),
  MSG VARCHAR2(200),
  CRT_TM   DATE DEFAULT SYSDATE
);

2.创建保存错误信息的存过

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE OR REPLACE PROCEDURE PROC_SAVE_ERRMSG(BIZCODE   IN VARCHAR2,
                                             ERRORLINE IN VARCHAR2,
                                             ERRORCODE IN VARCHAR2,
                                             MSG       IN VARCHAR2) IS
  /*必须要使用自治事务,否则commit会影响调用程序事务*/
  PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  INSERT INTO TBL_PROC_ERRMSG
    (BIZ_CODE, ERR_LINE, ERR_CODE, MSG)
  VALUES
    (BIZCODE, ERRORLINE, ERRORCODE, MSG);
  COMMIT;
END;

3.使用示例

1
2
3
4
5
--存过过程实现主体
EXCEPTION
  WHEN OTHERS THEN
    PROC_SAVE_ERRMSG(biz_code/*此变量用于查找错误记录标记*/,DBMS_UTILITY.format_error_backtrace,sqlcode,SQLERRM);
END;