I am trying to create a trigger that will send me an email should there ever be a problem with my database. Here's what I have so far:
CREATE OR REPLACE TRIGGER trg_mail_errors AFTER SERVERERROR ON DATABASE
declare
v_message VARCHAR2(1000);
v_sys_date VARCHAR2(30);
v_db_name VARCHAR2(10);
crlf VARCHAR2(2):= CHR(13)||CHR(10);
mail_conn utl_smtp.connection;
BEGIN
mail_conn :=utl_smtp.open_connection('mail.test.com',25);
v_message:= 'Date: '||to_char(sysdate,'dd Mon yy hh24:mi:ss' )||crlf
||'FROM: [email protected]'||crlf||'Subject: Database Error'||crlf ||'To: [email protected]'
||crlf||''|| crlf;
SELECT name into v_db_name from v$database;
SELECT to_char(sysdate,'MM-DD-YYYY hh24:mi:ss') INTO v_sys_date FROM dual;
SELECT 'Sent from trg_mail_errors script. Database error has occured on... <'||v_db_name||'> on '||v_sys_date INTO v_message FROM DUAL;
utl_smtp.helo(mail_conn,'mail.test.com');
utl_smtp.mail(mail_conn,'[email protected]');
utl_smtp.rcpt(mail_conn,'[email protected]');
utl_smtp.data(mail_conn,v_message);
utl_smtp.quit(mail_conn);
END;
/
I'm having a few issues:
1) the message does show up, but the To: and Subject: are blank
2) in an effort to tell which instance has shutdown, I am using the line: SELECT name into v_db_name from v$database; When I use this line, and try to complile it, I get an error saying the v$database table does not exist.
Any help would be appreciated.
Thanks in advance.
