习惯使用SQLPLUS管理Oracle的朋友肯定不会对使用show errors命令来确认PL/SQL匿名块或过程执行错误感到陌生。似乎在11g以前这是唯一的一种勘错途径了,可惜的是这部分show errors显示的错误信息往往不够全面同时也无法立即保存下来。11g r1中引入了新的错误日志特性,以便DBA或应用开发人员在调试PL/SQL程序时更高效地排除错误。通过set errorlogging命令可以在SQL执行期间将所有的错误记录存储在一个特定的错误日志表中。默认情况下,set errorlogging会将错误记录写入到名为SPERRORLOG的表中。可以客制化该表的表名,错误日志表记录错误的各种属性:1.引发错误用户的用户名;2.错误发生时间;3.包含引发错误语句的脚本名;4.用户自定义的标识符;5.SP2,ORA,PLS错误消息;6.具体引发错误的语句。
SQL> desc sperrorlog;
Name Null? Type
----------------------------------------- -------- ----------------------------
USERNAME VARCHAR2(256)
TIMESTAMP TIMESTAMP(6)
SCRIPT CLOB
IDENTIFIER VARCHAR2(256)
MESSAGE CLOB
STATEMENT CLOB
SQL> show errorlogging ;
errorlogging is OFF
/* 默认情况下错误日志功能是关闭的,我们需要手动打开它 */
SQL> set errorlogging on;
SQL> show errorlogging ;
errorlogging is ON TABLE SYS.SPERRORLOG
/* 可以看到成功打开错误日志功能,并会将后续的错误日志写入到当前用户(SYS)名下的SPERRORLOG表 */
SQL> select 1 from abc;
select 1 from abc
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> set linesize 200;
SQL> col username for a20;
SQL> col message for a40
SQL> col statement for a40;
/* session A */
SQL> select 1 from abc;
select 1 from abc
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select username,message,statement from sperrorlog;
USERNAME MESSAGE STATEMENT
-------------------- ---------------------------------------- ----------------------------------------
SYS ORA-00942: table or view does not exist select 1 from abc
/* 此时在session B中查询 */
SQL> select username,message,statement from sperrorlog;
no rows selected
/* 换而言之当errorlogging被激活后,发生错误时Oracle会将错误日志追加到SPERRORLOG表上,但并不commit; */
/* 这可能导致V$LOCK动态性能视图中显示大量的TX锁,虽然是虚惊:) */
By setting 'ERRORLOGGING' parameter in SQLPlus, a new table 'SPERRORLOG' will be created. SQLPlus will write session errors to an error table SPERRORLOG.
This feature very useful in order to track session errors.
Example:
SQL> set ERRORLOG ON
SQL> desc SPERRORLOG
Name Null? Type
----------------------------------------- -------- ----------------------------
USERNAME VARCHAR2(256)
TIMESTAMP TIMESTAMP(6)
script VARCHAR2(1024)
IDENTIFIER VARCHAR2(256)
MESSAGE CLOB
STATEMENT CLOB
SQL> SELECT USERNAME, STATEMENT, MESSAGE, TIMESTAMP FROM SPERRORLOG;
USERNAME STATEMENT MESSAGE
-----------------------------------------------------------------
TIMESTAMP
-----------------
SYS select * from table1 ORA-00942: table or view does not exist
16-DEC-07 02.21.43.000000 AM