数据库包的语法帮助

时间:2014-02-06 10:57:57

标签: plsql oracle11g package oracle-sqldeveloper

Oracle 11G数据库。我们有一个C#应用程序,可以与PL / SQL程序包通信,如果满足许多条件,则会向用户发送电子邮件。该示例主要涉及PERSON表和& APPOINTMENT表。

以下是我们目前的逻辑:

create or replace 
PACKAGE BODY email_send_pkg
IS 
PROCEDURE sp_email_reminder(p_HOURS IN NUMBER,
                                  p_message_cur IN OUT MessageCur)  
IS
BEGIN
    OPEN p_message_cur
     FOR 
SELECT a.person_id, 
       a.appoint_no,
       a.appoint_date_time,           
       p.name, 
       p.forename,
       p.surname,
       p.email_addre,
       l.location_name, 
       NVL(l.address1, ''), 
       NVL(l.address2, ''), 
       NVL(l.address3, '') 

  FROM appointment a,
       person p,
       location l
  WHERE a.appoint_date_time > SYSDATE   -- only send email for appointments in the future
  AND (sa.appoint_date_time - ((1/24) * p_HOURS)) < SYSDATE 
    AND a.cancel_date IS NULL -- only send email for appointment that have NOT been cancelled
    AND a.resched_date IS NULL -- only send email for appointment that have NOT been rescheduled
    AND p.person_id = a.person_id -- check correct record on PERSON table & APPOINTMENT table
    AND l.location_id = a.location_id -- check correct record on PERSON table & APPOINTMENT table
    AND email_addre IS NOT NULL -- only send email if email data exists for the person in question

    AND NOT EXISTS --avoid duplicate record
      ( SELECT *
          FROM email_message em
         WHERE em.appoint_no  = a.appoint_no
           AND em.message_type_id = 1  -- (type: example; appointment reminder)
      )
ORDER BY appoint_date_time ASC;
 END sp_email_reminder; 

假设满足所有条件,我们然后将数据添加到表中,稍后由C#应用程序轮询以发送电子邮件。

我需要实现更多逻辑以进行更多检查。我对PL / SQL很不熟悉,当然它的语法和需要添加更多逻辑的建议。

目前,如果某人出现在他们的预约中并且系统当前没有存储他们的电子邮件,则会在接待处要求他们进行预约。一旦它'添加到数据库(PERSON&amp; PERSON_AUDIT表更新)C#应用程序将轮询,看到新添加的电子邮件,并发送电子邮件给已经坐在等候室进行该约会的人....显然有点傻了不必要。

所以,我需要插入一些逻辑: 如果在APPOINT_DATE的PERSON_AUDIT表上EMAIL_ADDRE不再为空,则DONT发送电子邮件。

我还希望确保电子邮件永远不会在星期六或星期日或圣诞节等公共假期发送。 (但仍然提醒他们大约72小时通知)我想这个解决方案是创建一个完整的排除日期的简单数据库表,并检查,但我不知道如何用我目前的PL知识实际实现/ SQL。

关于当前逻辑的语法或注释的任何提示都会很棒! 感谢。

解决方案:(感谢Starfighter)

AND NOT EXISTS
    (  SELECT *
         FROM audituser.person_a pu
       WHERE pu.b_person_id = sa.person_id
      AND pu.b_email <> pu.a_email
        AND TRUNC(pu.a_last_upd_datetime) = TRUNC(sa.appoint_date)
        AND pu.a_email IS NOT NULL
   )

1 个答案:

答案 0 :(得分:1)

尝试在查询结尾添加以下内容:

and not exists (
  -- Avoid sending updates if person record changes on appointment date
  select 'X'
    from person_audit pa
   where a.person_id = pa.person_id
     -- Trunc is currently doing same day...
     -- this can be tweaked to look at range of minutes or hours
     and trunc(appoint_date_time)  trunc(last_upt_date_time)
     and pa.email_address is not null
)

这应该有效地省略在约会日期(或之后)更新人员记录(并且电子邮件地址不为空)的任何记录。