Oracle
 sql >> Database >  >> RDS >> Oracle

Come sviluppare un trigger post serverror in Oracle?

Non interrogare v$sql; ottieni l'istruzione usando ora_sql_txt.

CREATE OR REPLACE TRIGGER log_server_errors
AFTER SERVERERROR
ON DATABASE
DECLARE
sql_text ora_name_list_t;
stmt clob;
n number;
BEGIN
  n := ora_sql_txt(sql_text);
  if n > 1000 then n:= 1000; end if ;
  FOR i IN 1..n LOOP
     stmt := stmt || sql_text(i);
   END LOOP;

   INSERT INTO servererror_log
   (error_datetime, error_user, db_name,
    error_stack, captured_sql)
   VALUES
   (systimestamp, sys.login_user, sys.database_name,
   dbms_utility.format_error_stack, stmt);
   commit;
 END log_server_errors;
 /

Quindi:

SQL> select * from c;

Questo produce:

select * from c
              *
ERROR at line 1:
ORA-00942: table or view does not exist

Questo ora può essere interrogato:

select * from servererror_log;

Per produrre:

ERROR_DATETIME
---------------------------------------------------------------------------
ERROR_USER                     DB_NAME
------------------------------ ---------
ERROR_STACK
--------------------------------------------------------------------------------
CAPTURED_SQL
--------------------------------------------------------------------------------
11-FEB-09 02.55.35.591259 PM
SYS                            TS.WORLD
ORA-00942: table or view does not exist
select * from c