oracle中的更新触发器问题

Issue in update trigger in oracle

只要我的 cust_bug_data table 中的 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;

您可以按如下方式更改触发代码:

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;