甲骨文PL/SQL。将邮件地址循环输出传递给 oracle 电子邮件程序

Oracle PL/SQL. Pass mail address loop output to oracle email procedure

我有一个发送邮件的程序。我想通过从视图中选择电子邮件地址并将地址传递给我的邮件程序来发送邮件。

此代码的工作原理是它从我的数据库中输出多个电子邮件地址和事件编号。

create or replace procedure SEND_REMINDER_MAIL as

CURSOR c1 IS
SELECT contact_email
 FROM TEST.incidents_view
WHERE updated <= current_timestamp - interval '1' minute
and status_code = '100';

cursor c2 is
SELECT incident_number
FROM TEST.incidents_view WHERE updated <= current_timestamp - interval '1' minute
and status_code = '100';

v_contact_email varchar2(300);
v_incno VARCHAR2(10);

BEGIN
   -- Open the cursor and loop through the records
   OPEN c1;

      FETCH c1 INTO v_contact_email;
      EXIT WHEN c1%NOTFOUND;
      -- Print  values
      dbms_output.put_line(v_contact_email);
  end loop;
      CLOSE c1;

   open c2;
LOOP
   fetch c2 into v_incno;
   EXIT WHEN c2%NOTFOUND;
dbms_output.put_line(v_incno);
   end loop;
    CLOSE c2;
end;

/

不过,我需要做的是仅将电子邮件地址传递到现有的电子邮件程序中,以向出现在输出生成的列表中的任何人发送电子邮件。

这是程序的下一部分,我已经使用 dbms_output 来测试和验证电子邮件地址是否正确生成并传递到 v_contact_email。现在,当我尝试发送邮件时,只有一个地址被传入:

send_mail.send(
ToList=>             v_contact_email,
Subject=>            'Ticket closing warning.',
Body=>               'Please note, your ticket '|| v_incno ||' will be subject to automatic closure',
FromEmail=>          'donotreply@test.com.au',
FromHost=>           'emailsrv',
SMTPServer=>         'emailsrv',);
close c1;
close c2;
End;
/

它没有发送到显示已正确传递到 v_contact_email 的几封电子邮件。它只发送一封电子邮件,仅此而已。

为什么循环进入 v_contact_email 的多个电子邮件地址不会导致发出多封电子邮件,而不是一封?

我该如何修复它,以便:

send_mail.send(
ToList=>             v_contact_email,
Subject=>            'Ticket closing warning.',
Body=>               'Please note, your ticket '|| v_incno ||' will be subject to automatic closure',
FromEmail=>          'donotreply@test.com.au',
FromHost=>           'emailsrv',
SMTPServer=>         'emailsrv',);
close c1;
close c2;
End;
/

部分代码正确循环了整个结果集?而不是只获取一个地址并发送一封电子邮件,然后什么都不做?

您发布的第一个代码不完全有效;它第一个游标缺少循环。如果您修复该问题,结果将是:

  • 一堆电子邮件地址
  • 一堆事件编号

第一个循环获取电子邮件地址,因此 v_contact_email 仅包含最后获取的地址。事件编号也是如此。

一个选项是您希望 嵌套 那些循环,像这样(我使用游标 FOR 循环,因为它们更容易维护显式声明的游标,您必须声明(以及游标变量、打开、循环、注意退出循环、关闭游标)——如果您使用游标 FOR 循环,Oracle 会为您完成大部分这些事情。

begin
  for cur_r in (select contact_email from incidents_view where ...) loop
    for cur_i in (select incident_number from incidents_view where ...) loop
      send_mail.send(ToList  => cur_r.contact_email,
                     Subject => 'Ticket closing warning.',
                     Body    => 'Please note, your ticket '|| cur_i.incident_number ...
                    );
    end loop;
  end loop;
end;

但是,为什么你有两个光标?它们看起来一样(除了它们 select 的内容),但是 - FROM 子句是相等的,WHERE 子句是相等的……为什么不只使用一个游标?例如

begin
  for cur_r in (select contact_email, incident_number
                from incidents_view where ...
               ) loop
    send_mail.send(ToList  => cur_r.contact_email,
                   Subject => 'Ticket closing warning.',
                   Body    => 'Please note, your ticket '|| cur_r.incident_number ...
                  );
  end loop;
end;

在 Littlefoot 的帮助下,这就是我想出的,完全符合我的要求。

create or replace procedure SEND_MAIL_REMINDER as
    begin
      for cur_r in (SELECT customer_email, incident_number
    FROM incidents_view
    WHERE updated <= current_timestamp - interval '24' hour
    and status = '20') loop
    send_mail.send(
    ToList=>             cur_r.customer_email,
    Subject=>            'Ticket Closure Warning.',
    Body=>               'Please note, your ticket '|| cur_r.incident_number ||' has been in status 20 for some time.',
    FromEmail=>          'donotreply@blah.com',
    FromHost=>           'mailsrv',
    SMTPServer=>         'mailsrv',
    AttachList=>         '',
    Directory=>          '');
    end loop;
    end;
    /