oracle 中的更新触发器问题
Posted
技术标签:
【中文标题】oracle 中的更新触发器问题【英文标题】:Issue in update trigger in oracle 【发布时间】:2020-04-17 13:06:55 【问题描述】:每当我的 cust_bug_data 表中的 ANALYZED_BY 列有更新时,我都会使用以下触发器发送电子邮件。我面临的问题是触发发送电子邮件,即使在 cust_bug_data 中更新了其他列。
假设我的 cust_bug_data 类似于“create table cust_bug_data(producd_id number, subject varchar(10), ANALYZED_BY varchar2(1))”
我的要求是,只要 ANALYZED_BY 列值有更新,就应该执行触发器,而对于其他列的值更新,它不应该被执行。
================================================ =============================
create table cust_bug_data(producd_id number, subject varchar(10), ANALYZED_BY varchar2(1))
--Below is the trigger
create or replace TRIGGER CUST_BUG_SEND_ASSIGNMENT_EMAIL
after UPDATE of ANALYZED_BY ON cust_bug_data
for each row
DECLARE
email_id_val varchar2(50);
l_plain CLOB;
l_html CLOB;
cust_bug_number number;
bug_subject varchar2(100);
current_user_email varchar2(100);
BEGIN
THEN
CASE
WHEN UPDATING('ANALYZED_BY') THEN
IF :NEW.ANALYZED_BY is not null THEN
--Get email of receiver
SELECT EMAIL_ID into email_id_val FROM CUST_BUG_USER_MGR_RLTN where GUID = :NEW.ANALYZED_BY;
--Get email of sender
current_user_email := APEX_CUSTOM_AUTH.GET_USER;
wwv_flow_api.set_security_group_id;
dbms_lob.createTemporary(l_plain,true);
dbms_lob.createTemporary(l_html,true);
dbms_lob.append(l_plain,'New Bugs Generated'||utl_tcp.crlf);
dbms_lob.append(l_plain,''||utl_tcp.crlf||utl_tcp.crlf);
dbms_lob.append(l_plain,:new.bug_number||utl_tcp.crlf);
dbms_lob.append(l_html,'<tr><td><a href="https://bug.oraclecorp.com/pls/bug/webbug_print.show?c_rptno='||:new.bug_number||'">'||:new.bug_number||'</a>'||:new.subject||'</td></tr>'||utl_tcp.crlf);
dbms_lob.append(l_html,utl_tcp.crlf||'</table>');
apex_mail.send(
p_to => email_id_val,
p_from => current_user_email,
p_subj => 'Action Required: New customer bugs to be Analyzed in Bug Insight',
p_body => l_plain,
p_body_html => l_html
);
dbms_lob.freeTemporary(l_plain);
dbms_lob.freeTemporary(l_html);
END IF;
END CASE;
END;
【问题讨论】:
【参考方案1】:您可以按如下方式更改触发代码:
create table cust_bug_data(producd_id number, subject varchar(10), ANALYZED_BY varchar2(1))
--Below is the trigger
create or replace TRIGGER CUST_BUG_SEND_ASSIGNMENT_EMAIL
after UPDATE of ANALYZED_BY ON cust_bug_data
for each row
when ((NEW.ANALYZED_BY <> OLD.ANALYZED_BY OR OLD.ANALYZED_BY IS NULL)
AND NEW.ANALYZED_BY is not null) -- added this condition
DECLARE
email_id_val varchar2(50);
l_plain CLOB;
l_html CLOB;
cust_bug_number number;
bug_subject varchar2(100);
current_user_email varchar2(100);
BEGIN
--THEN
--CASE
--WHEN UPDATING('ANALYZED_BY') THEN
--IF :NEW.ANALYZED_BY is not null THEN -- removed 4 lines
--Get email of receiver
SELECT EMAIL_ID into email_id_val FROM CUST_BUG_USER_MGR_RLTN where GUID = :NEW.ANALYZED_BY;
--Get email of sender
current_user_email := APEX_CUSTOM_AUTH.GET_USER;
wwv_flow_api.set_security_group_id;
dbms_lob.createTemporary(l_plain,true);
dbms_lob.createTemporary(l_html,true);
dbms_lob.append(l_plain,'New Bugs Generated'||utl_tcp.crlf);
dbms_lob.append(l_plain,''||utl_tcp.crlf||utl_tcp.crlf);
dbms_lob.append(l_plain,:new.bug_number||utl_tcp.crlf);
dbms_lob.append(l_html,'<tr><td><a href="https://bug.oraclecorp.com/pls/bug/webbug_print.show?c_rptno='||:new.bug_number||'">'||:new.bug_number||'</a>'||:new.subject||'</td></tr>'||utl_tcp.crlf);
dbms_lob.append(l_html,utl_tcp.crlf||'</table>');
apex_mail.send(
p_to => email_id_val,
p_from => current_user_email,
p_subj => 'Action Required: New customer bugs to be Analyzed in Bug Insight',
p_body => l_plain,
p_body_html => l_html
);
--END IF;
--END CASE; -- removed this two
dbms_lob.freeTemporary(l_plain);
dbms_lob.freeTemporary(l_html);
END;
【讨论】:
以上是关于oracle 中的更新触发器问题的主要内容,如果未能解决你的问题,请参考以下文章
当我调用触发器更新 STUDENT 表中的总数时,Oracle 中的 SQL 出错
Oracle 触发器检查一个表中的课程持续时间,如果课程是特定日期,则阻止更新