Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Wie entwickle ich einen After-Server-Trigger in Oracle?

v$sql nicht abfragen; Holen Sie sich die Anweisung mit 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;
 /

Dann:

SQL> select * from c;

Dies erzeugt:

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

Das kann jetzt abgefragt werden:

select * from servererror_log;

Zu produzieren:

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