Skip to content

Commit 0b1ebfa

Browse files
committed
Procedure with Update and email
This procedure I wrote for a certain update statement that runs and then to send a mail from the database. It used to run as a shell script via cron, but changed it to run so it is always in the DB when we refresh to test etc
1 parent 3fbe72f commit 0b1ebfa

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

proc_update_mail.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
REM Script Name : tp_datamgt_bonds.sql
2+
REM Author : Craig Richards
3+
REM Created : 20 May 2013
4+
REM Last Modified :
5+
REM Version : 1.0
6+
REM
7+
REM Modifications :
8+
REM
9+
REM Description : This procedure updates data
10+
11+
CREATE OR REPLACE PROCEDURE tp_datamgt_bonds
12+
AUTHID CURRENT_USER
13+
AS
14+
15+
--Variable Declaration
16+
17+
num_rows NUMBER;
18+
v_From VARCHAR2(80) := '[email protected]';
19+
v_Recipient VARCHAR2(80) := '[email protected]';
20+
v_Subject VARCHAR2(80) := 'Your Update Statement';
21+
v_Mail_Host VARCHAR2(30) := 'mailhost';
22+
v_Mail_Conn UTL_SMTP.Connection;
23+
CRLF VARCHAR2(2) := CHR(13)||CHR(10);
24+
25+
BEGIN
26+
27+
-- Update SQL Statement
28+
29+
UPDATE table
30+
SET active_flag = 'N'
31+
WHERE a=b;
32+
33+
num_rows := SQL%ROWCOUNT;
34+
35+
-- Send an email with the amount of rows that have been updated
36+
37+
v_Mail_Conn := UTL_SMTP.Open_Connection(v_Mail_Host, 25);
38+
UTL_SMTP.Helo(v_Mail_Conn, v_Mail_Host);
39+
UTL_SMTP.Mail(v_Mail_Conn, v_From);
40+
UTL_SMTP.Rcpt(v_Mail_Conn, v_Recipient);
41+
UTL_SMTP.Data(v_Mail_Conn,'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || CRLF ||
42+
'From: ' || v_From || CRLF ||
43+
'Subject: '|| v_Subject || CRLF ||
44+
'To: ' || v_Recipient || CRLF ||CRLF ||'Procedure Ran and there were ' || num_rows || ' rows updated '|| CRLF || CRLF || 'Regards' ||CRLF ||'DBA Team');
45+
46+
-- Exception Code
47+
48+
EXCEPTION
49+
WHEN UTL_SMTP.Transient_Error OR UTL_SMTP.Permanent_Error THEN
50+
RAISE_APPLICATION_ERROR(-20000, 'Unable to send mail: '||SQLERRM);
51+
DBMS_OUTPUT.PUT_LINE (num_rows || ' Rows Updated');
52+
END;
53+
/

0 commit comments

Comments
 (0)