以下两个过程呢,其实是辅助过程,一个是完成写日志,一个是完成发邮件,在以后的程序中,可能会经常用到。
1、写日志过程
name:sp_Write_log
parameter:textContext in varchar2 日志内容
create date:2003-06-01
creater:chen jiping
desc: •写日志,把内容记到服务器指定目录下
•必须配置Utl_file_dir初始化参数,
并保证日志路径与Utl_file_dir路径一致或者是其中一个
create or replace PROCEDURE sp_Write_log(textContext VARCHAR2)
IS
file_handle UTL_FILE.file_type;
Write_content VARCHAR2(1024);
Write_file_name VARCHAR2(50);
BEGIN
--打开文件
--Write_file_name := rtrim(to_char(SYSDATE,'YYYY-MM-DD'))||'.log';
Write_file_name := 'db108_alert.log';
file_handle := UTL_FILE.FOPEN('/u01/product/admin/ora81/logs',Write_file_name,'a');
Write_content := to_char(SYSDATE,'yyyy-mm-dd hh24:mi:ss')||'||'||textContext;
--写文件
IF UTL_FILE.IS_OPEN(file_handle) THEN
UTL_FILE.PUT_LINE(file_handle,Write_content);
END IF;
--关闭文件
UTL_FILE.Fclose(file_handle);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.IS_OPEN(file_handle) THEN
UTL_FILE.Fclose(file_handle);
END IF;
END sp_Write_log; |
2、发送Email的过程
name:sp_Send_mail
parameter: Rcpter in varchar2 接收者邮箱
Mail_Content in Varchar2 邮件内容
create date:2003-06-01
creater:chen jiping
desc: •发送邮件到指定邮箱
•只能指定一个邮箱,如果需要发送到多个邮箱,需要另外的辅助程序
create or replace procedure sp_send_mail(
Rcpter IN VARCHAR2,
Mail_Content IN VARCHAR2)
IS
conn utl_smtp.connection;
PROCEDURE send_header(NAME IN VARCHAR2, header IN VARCHAR2) AS
BEGIN
utl_smtp.write_data(conn, NAME || ': ' || header || utl_tcp.CRLF);
END;
BEGIN
conn := utl_smtp.open_connection('smtp.ur.net.cn');
utl_smtp.helo(conn, 'oracle');
utl_smtp.mail(conn, 'oracle info');
utl_smtp.rcpt(conn, Rcpter);
utl_smtp.open_data(conn);
send_header('From', 'Oracle Database');
send_header('To', '"Recipient" <'||Rcpter||'>');
send_header('Subject', 'Hello');
utl_smtp.write_data(conn, utl_tcp.CRLF || Mail_Content);
utl_smtp.close_data(conn);
utl_smtp.quit(conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
utl_smtp.quit(conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
NULL; -- When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The quit call will raise an
-- exception that we can ignore.
END;
raise_application_error(-20000,
'Failed to send mail due to the following error: ' || SQLERRM);
END sp_send_mail; | |