Oracle PL / SQL向用户发送有关数据更改的电子邮件通知

时间:2018-10-22 19:15:42

标签: oracle email notifications

我有批量数据上传过程,通过该过程可以将数据加载到名为 TICKETS 的表中。下面是带有伪数据的表结构:

+----------+---------------+--------------+
| TicketID |     Email     | Payment_Hold |
+----------+---------------+--------------+
|        1 | smith@abc.com | YES          |
|        2 | john@abc.com  | NO           |
|        3 | mary@abc.com  | YES          |
|        4 | jane@abc.com  | YES          |
|        5 | king@abc.com  | NO           |
+----------+---------------+--------------+

在上载过程之后,几行中的数据被更改,将插入新行或更新现有行。下面是上传后的数据快照:

+----------+---------------+--------------+
| TicketID |     Email     | Payment_Hold |
+----------+---------------+--------------+
|        1 | blake@abc.com | YES          |
|        2 | john@abc.com  | YES          |
|        4 | jane@abc.com  | NO           |
|        5 | scott@abc.com | NO           |
|        6 | jim@abc.com   | YES          |
+----------+---------------+--------------+

话虽如此,每当两列中的数据发生变化时,我都会尝试将通知发送给票务人员,

  1. 对于特定的票证ID,如果某人被更改并且该票证具有付款保留权,则向处理该票证的新人发送通知,并且
  2. 如果更改了票证ID上的保留付款,则向处理票证的人发送通知。

我认为实现此目标的最佳方法是创建基于价值的审计表,并在 TICKETS 表上触发(插入或更新后)以捕获更改。以下是 TICKETS_AUDIT_TABLE

+-----+----------+---------------+---------------+------------------+------------------+
| ID  | TicketID |   Old Email   |   New Email   | Old_Payment_Hold | New_Payment_Hold |
+-----+----------+---------------+---------------+------------------+------------------+
| 100 |        1 | smith@abc.com | blake@abc.com | YES              | YES              |
| 200 |        2 | john@abc.com  | john@abc.com  | NO               | YES              |
| 300 |        4 | jane@abc.com  | jane@abc.com  | YES              | NO               |
| 400 |        5 | king@abc.com  | scott@abc.com | NO               | NO               |
| 500 |        6 | NULL          | jim@abc.com   | NULL             | YES              |
+-----+----------+---------------+---------------+------------------+------------------+

对于电子邮件,我正在使用下面的pl / sql代码:

DECLARE 
    vemailbody CLOB := NULL; 
    CURSOR c1 IS 
      SELECT * 
      FROM   tickets_audit_table 
      ORDER  BY id; 
BEGIN 
    wwv_flow_api.set_security_group_id; 

    vemailbody := vemailbody 
                  || '<table><tr><th>Ticket Update</th></tr><tr> 
              <th><b>Ticket ID</b></th> 
              <th>Old Email</th> 
              <th>New Email</th> 
              <th>Old Payment Hold Flag</th> 
              <th>New Payment Hold Flag</th> </tr>'; 

    FOR rec IN c1 LOOP 

        vemailbody := vemailbody || '<tr> <td>' || rec.ticketid 
                             ||'</td> <td>' || rec.old_email 
                             ||'</td> <td>' || rec.new_email 
                             ||'</td> <td>' || rec.old_payment_hold 
                             ||'</td> <td>' || rec.new_payment_hold 
                             ||'</td> </tr>'|| utl_tcp.crlf; 
    END LOOP; 

    vemailbody := vemailbody || '</table>' || utl_tcp.crlf; 
    vemailbody := vemailbody || '<br>' || utl_tcp.crlf; 
    vemailbody := vemailbody || '</td>'|| utl_tcp.crlf; 
    vemailbody := vemailbody || '</tr>'|| utl_tcp.crlf; 
    vemailbody := vemailbody || '</table>'|| utl_tcp.crlf; 

    dbms_output.Put_line(vemailbody); 

    apex_mail.Send(p_to => 'richa@abc.com', 
                   p_from => 'Test Mail <test@abc.com>',
               p_subj => 'Ticket update',
           p_body => vemailbody, 
           p_body_html => vemailbody); 

    apex_mail.push_queue; 
END; 

这里的问题是,先前收到电子邮件的用户会多次触发同一封电子邮件。不知道如何根据以下条件发送电子邮件通知: 例如:考虑票证ID = 1

  • 如果(Old_Email不等于New_Email)AND(Old_Payment_Hold不等于New_Payment_Hold),则仅将电子邮件发送给New_Email
  • 如果已经发送了票证ID = 1的电子邮件,其中(New_Email = Old_Email)和(Old_Payment_Hold = New_Payment_Hold),则不要重新发送电子邮件。

我真的很感谢为此提供的任何帮助。

谢谢,
里查

0 个答案:

没有答案