【问题标题】:Oracle PL/SQL. Pass mail address loop output to oracle email procedure甲骨文 PL/SQL。将邮件地址循环输出传递给 oracle 电子邮件程序
【发布时间】:2019-04-10 14:06:40
【问题描述】:

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

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

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;
/

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

【问题讨论】:

    标签: oracle loops plsql cursor procedure


    【解决方案1】:

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

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

    第一个循环获取电子邮件地址,因此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;
    

    但是,为什么你有两个光标?它们看起来一样(除了他们选择的内容),但是 - 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,你是一个绝对的传奇人物,我非常感谢你。您为我解决了这个问题,并且您给了我足够的洞察力来阅读和编辑我已经编写的代码,只使用一个光标。干得好!!
    【解决方案2】:

    在您的帮助 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;
        /
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 2013-01-06
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多