我正在为Oracle DB编写一个触发器来发送电子邮件,然后更新该记录的列,确认该电子邮件已发送。我被建议创建一个光标来获取每一行,然后收集电子邮件的信息,发送电子邮件,更新记录,然后循环重复。下面的代码是我到目前为止的代码。
CREATE OR REPLACE TRIGGER "SEND_EMAIL"
After INSERT OR UPDATE OF ISSUE_ADDED_TO_ALM ON DB_TABLE FOR EACH ROW
DECLARE
l_table DB_TABLE%rowtype;
l_body varchar2(4000);
l_to_address varchar2(2000);
l_from varchar2(200);
l_name varchar2(100);
l_summary varchar2(1000);
l_description varchar2(4000);
l_ALM_ID varchar2(100);
l_subject varchar2(400);
l_added_to_alm varchar2(200);
l_SID varchar2(200);
CURSOR cur_ADDED_TO_ALM IS
select * FROM DB_TABLE where ISSUE_ADDED_TO_ALM = '1' and EMAIL_NOTIFICATION = '0';
BEGIN
OPEN cur_ADDED_TO_ALM;
LOOP
Fetch cur_ADDED_TO_ALM into l_table;
Exit when cur_ADDED_TO_ALM%NOTFOUND;
l_from := 'Data Quality IMS Team';
select ISSUE_REQUESTER into l_SID from DB_TABLE;
select emp_email_name,concat(concat(emp_first_name,' '),emp_last_name) into l_to_address, l_name from telephone_book where emp_id = l_SID;
select ISSUE_SUMMARY,ISSUE_DESCRIPTION,ALM_ISSUE_ID into l_summary, l_description, l_ALM_ID from DB_TABLE where ISSUE_ADDED_TO_ALM = '1' and EMAIL_NOTIFICATION = '0';
l_subject := l_ALM_ID + 'Request has been created.';
l_body := '<style type="text/css">
p{font-family: Calibri, Arial, Helvetica, sans-serif;
font-size:12pt;
margin-left:30px;
}
</style>';
l_body := l_body || '<p>Your request has been created.</p>';
l_body := l_body || '<p>Data Quality Center received the following request:</p>';
l_body := l_body || '<p>Request ID: '|| l_ALM_ID ||'</p>';
l_body := l_body || '<p>Request Title: '|| l_summary||'</p>';
l_body := l_body || '<p>Request Description: '|| l_description||'</p>';
HTMLDB_MAIL.SEND(
P_TO => l_to_address,
P_FROM => l_from,
P_BODY => l_body,
P_BODY_HTML => l_body,
P_SUBJ => l_subject);
wwv_flow_mail.push_queue(
P_SMTP_HOSTNAME => 'mail.sever_name.net',
P_SMTP_PORTNO => '5'
);
END LOOP;
update DB_TABLE set EMAIL_NOTIFICATION = '1' where ALM_ISSUE_ID = l_ALM_ID
IF cur_ADDED_TO_ALM%ISOPEN then
CLOSE cur_ADDED_TO_ALM;
END IF;
end;
我收到以下错误: ORA-04091:表server.DB_Table正在变异,触发器/函数可能看不到它ORA-06512:在“server.SEND_EMAIL”,第15行ORA-06512:在“server.SEND_EMAIL”,第18行ORA-04088:错误期间执行触发器'server.SEND_EMAIL'
答案 0 :(得分:4)
我提供这些解决方案。
UPDATE
激活触发器的表格。您应该使用:new.EMAIL_NOTIFICATION := 1;
SELECT
激活触发器的表格。这在逻辑上没有意义。这就是你得到“变异表”错误的原因。表正在改变(变异)并且您想要阅读它,这将产生模糊的结果。 不要通过自主交易来解决这个问题。某个地方的某个人会给你这个建议。那就是把自己挖到更深的洞里。总的来说,这是一个糟糕的主意。出于这样的目的,你真的不想这样做。考虑一下,用户进行插入或更新,必须等到数据库发送所有必要的电子邮件。如果触发器中出现任何异常,则事务将失败,您必须回滚。
更好的计划