--建立错误日志表 create table PUB_PROC_ERR_LOG ( LOG_ID NUMBER, MODULE_NAME VARCHAR2(100), PROC_NAME VARCHAR2(100), ERR_TIME DATE, SQL_CODE VARCHAR2(50), SQL_ERRM VARCHAR2(100), ERR_CONTENT VARCHAR2(500) ); comment on column PUB_PROC_ERR_LOG.LOG_ID is '主键'; comment on column PUB_PROC_ERR_LOG.MODULE_NAME is '模块名称'; comment on column PUB_PROC_ERR_LOG.PROC_NAME is '存储过程名称'; comment on column PUB_PROC_ERR_LOG.ERR_TIME is '报错时间'; comment on column PUB_PROC_ERR_LOG.SQL_CODE is 'SQLCODE'; comment on column PUB_PROC_ERR_LOG.SQL_ERRM is 'SQLERRM'; comment on column PUB_PROC_ERR_LOG.ERR_CONTENT is '报错的具体行'; --表主键的序列 create sequence SEQ_RECORD_PROC_ERR minvalue 1 maxvalue 9999999999999999999999999999 start with 21 increment by 1 cache 20; --通用记录错误存储过程 CREATE OR REPLACE PROCEDURE record_proc_err_log(module_name varchar2, proc_name varchar2, v_SQLCODE varchar2, v_SQLERRM varchar2, v_err_line varchar2) is PRAGMA AUTONOMOUS_TRANSACTION; BEGIN insert into pub_proc_err_log (log_id, module_name, proc_name, err_time, sql_code, sql_errm, err_content) values (seq_record_proc_err.nextval, module_name, proc_name, sysdate, v_SQLCODE, v_SQLERRM, v_err_line); commit; END record_proc_err_log; --进行测试 create or replace procedure test_p1 is begin execute IMMEDIATE 'select from test'; exception when others then record_proc_err_log('模块名','test_p1',SQLCODE,SQLERRM, substr(dbms_utility.format_error_backtrace, 1, 400)); end test_p1; SQL> col proc_name format a8; SQL> col err_time format a10; SQL> col sql_code format a5; SQL> col SQL_ERRM format a22; SQL> col ERR_CONTENT format a42; SQL> select proc_name,err_time,sql_code,SQL_ERRM,ERR_CONTENT from pub_proc_err_log; PROC_NAM ERR_TIME SQL_C SQL_ERRM ERR_CONTENT -------- ---------- ----- ---------------------- ------------------------------------------ test_p1 08-12月-14 -936 ORA-00936: 缺失表达式 ORA-06512: 在 "LCAM_TEST.TEST_P1", line 3