有的时候,系统会出现一些错误(比如:ORA-01652: unable to extend temp segment by 128 in tablespace TEMP),
但是我们不知道是哪个SQL出问题了,这个时候可以用创建触发器捕获引发错误的SQL
create table error_tab(username varchar2(100),errorcode varchar2(200),errortext varchar2(200),sql_text varchar2(4000));
create or replace trigger error_trigger
after servererror on database
declare
n number;
full_text varchar2(4000);
sql_text ora_name_list_t;
d_current_nr_error number := ora_server_error(1);
ora_server_error_msg varchar2(100);
begin
n := ora_sql_txt(sql_text);
ora_server_error_msg := SQLERRM(-d_current_nr_error);
FOR i IN 1 .. n LOOP
full_text := full_text || sql_text(i);
END LOOP;
insert into error_tab
values
(user, d_current_nr_error, ora_server_error_msg, full_text);
end;
/
比如
SQL> select 1/0 from dual;
select 1/0 from dual
*
第 1 行出现错误:
ORA-01476: 除数为 0
SQL> select * from error_tab;
USERNAME ERRORCODE ERRORTEXT SQL_TEXT
-------------------- -------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------
SYS 1476 ORA-01476: 除数为 0 select 1/0 from dual