SQL存储过程发送电子邮件(没有结果时的消息)?

时间:2013-08-21 21:26:40

标签: sql stored-procedures oracle-sqldeveloper smtpclient

我正在oracle sql中编写一个程序,它向客户发送电子邮件,提醒他们某些重要信息。现在我有正确的程序,电子邮件可以成功发送。我只是想知道如何创建一个消息而不是一个空的结果表,如果我没有任何东西可以显示没有任何事件与“待确定”的人一起被确定。(但我想保持循环,所以当有数据时它会显示)

我尝试了if语句并计算结果但是在编译代码时它一直给我错误。我不能有任何条件,如cnt = 0然后....在for语句和循环的中间。

这是我的代码:

CREATE OR REPLACE
PROCEDURE DOM.TEST_SCHED_EMAIL(
p_month DATE)
IS
conn utl_smtp.connection;
v_message     VARCHAR2(12000);
v_log         VARCHAR2(500);
email_id      NUMBER;
rec_person_id NUMBER;
email         VARCHAR2(100);
full_name     VARCHAR2(100);
CURSOR recipients_cursor
IS
SELECT DISTINCT p.person_id,
  email_address,
  p.fname
  ||' '
  ||p.lname AS full_name
FROM per.person p
WHERE person_id =67786895;
BEGIN
--  dbms_output.enable (50000);
OPEN recipients_cursor;
LOOP
FETCH recipients_cursor
INTO rec_person_id,
  email,
  full_name;
EXIT
WHEN recipients_cursor%NOTFOUND;
BEGIN
  v_message   := '<HTML><BODY>';
  v_message   := v_message ||'There are one or more event with person assigned as To Be Determined. These events are under 90 days of its scheduled date. Please take appropriate action for the events listed below'||'.';
  v_message   := v_message ||'<table>';
  v_message   := v_message ||'<br><br>';
  v_message   := v_message ||'<b>'||'EVENTS WITH ASSIGNED PERSON: TO BE DETERMINED'||'</b><br>';
  v_message   := v_message ||'<table border="1">';
  v_message   := v_message ||'<tr><td width="100">'||'Lname'||'</td><td width="100">'||'Fname'||'</td><td width="250">'||'Event Date'||'</td><td width="100">'||'Event Name'||'</td></td><td width="100">'||'Meridian'||'</td></tr>';
  FOR cur_rec IN
  (SELECT a.fname,
    a.lname,
    b.event_date,
    b.event_name,
    b.meridian
  FROM cal.appt_person_event_statustype b
  JOIN per.person a
  ON a.person_id=b.person_id
  WHERE upper(a.lname) LIKE '%TO BE%'
  AND b.event_date >= sysdate
  AND b.event_date <= sysdate+90
  )
  LOOP
    v_message := v_message||'<tr border="1"><td width="100">'||cur_rec.lname||'</td><td width="100">'||cur_rec.fname||'</td><td width="250">'||cur_rec.event_date||'</td><td width="100">'||cur_rec.event_name||'</td><td width="100">'||cur_rec.meridian||'</td></tr>';
  END LOOP;
  v_message := v_message ||'</table>';
  v_message := v_message ||'<br><br>';
  v_message := v_message||'</BODY></HTML>';
  conn      := per.mail.begin_mail( sender_email => '2222@uiowa.edu', sender_name => 'jia', recip_email => email, recip_name => full_name, subject => 'Moonlighting/Outreach Verification for '||TO_CHAR(p_month, 'MM/YYYY'), mime_type => per.mail.MULTIPART_MIME_TYPE);
  v_message := REPLACE(v_message,'!','');
  per.mail.attach_text( conn => conn, data => v_message, mime_type => 'text/HTML');
  per.mail.end_mail( conn => conn );
EXCEPTION
WHEN OTHERS THEN
  per.mail.mail('2222@uiowa.edu', 'Moonlighting Notify', '2222@uiowa.edu', 'IntMed-OracleHelp', 'Moonlighting Notification Error', 'rec_person_id='||rec_person_id||utl_tcp.CRLF ||'Email:'||email||utl_tcp.CRLF ||'Name:'||full_name||utl_tcp.CRLF ||'Error Code:'||SQLCODE);
  END;
END LOOP;
END;

任何想法的家伙?提前谢谢!

0 个答案:

没有答案