2014-02-06 41 views
0

Oracle 11G數據庫。我們有一個C#應用程序,它與PL/SQL程序包有關,如果滿足許多條件,它會向用戶發送電子郵件。這個例子主要處理一個PERSON表&一個APPOINTMENT表。數據庫包的語法幫助

這是我們目前擁有的邏輯:假設所有條件都滿足,那麼我們將數據添加到表,這是由C#應用程序後調查發出的電子郵件

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; 

我需要實施更多的邏輯來做更多的檢查。我對PL/SQL非常不熟悉,當然這是語法,需要關於添加更多邏輯的建議。

目前,如果某人出現了他們的約會,並且系統目前沒有存儲他們的電子郵件,他們會在接待處詢問。一旦它被添加到數據庫中(PERSON & PERSON_AUDIT表已更新)C#應用程序將輪詢,查看新添加的電子郵件,併發送電子郵件給已經坐在候診室進行該約會的人員。顯然有點傻了&不用。

因此,我需要插入一些邏輯,如下所示: 如果EMAIL_ADDRE在APPOINT_DATE上的PERSON_AUDIT表上沒有較長的空值,則不要發送電子郵件。

我還想確保電子郵件從不在星期六或星期天或聖誕節等公共假期發送。 (但仍然給他們大約72小時的通知)我想這個解決方案將創建一個簡單的數據庫表中充滿排除日期,並檢查反對,但我不知道如何實際執行這與我目前的PL知識/ SQL。

關於當前邏輯的語法或註釋的任何提示都會很棒! 謝謝。

解決方案:(感謝星際戰鬥機)

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

嘗試添加以下到您的查詢的末尾:

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 
) 

這應該有效地忽略其中的個人記錄被更新的任何記錄(和該電子郵件地址不爲空)在約會日期(或之後)。

+0

真棒,我最終得到了這個,稍有不同,但你的答案很好。謝謝。 OP編輯。 – GrumP