1

我有一个 PL/SQL 电子邮件包,如下所示:

create or replace package mail_pkg 
as
PRAGMA SERIALLY_REUSABLE; -- this avoids ORA-04068 error
type array is table of varchar2(255);

procedure send( p_sender_email in varchar2,
            p_from         in varchar2 default null,
            p_to           in array default array(),
            p_cc           in array default array(),
            p_bcc          in array default array(),
            p_subject      in varchar2 default null,
            p_body         in clob default null);

示例用法是:

 begin
    mail_pkg.send( p_sender_email => 'tim@company1.com',
               p_from => 'John Smith <johns@company2.com>',
               p_to => mail_pkg.array( 'greg@company3.com','sarah@company4.com'),
               p_cc => mail_pkg.array( 'admin@company5.com' ), 
               p_bcc => mail_pkg.array( 'sue@company5.com' ), 
               p_subject => 'This is my subject', 
               p_body => 'Hello, this is the mail you requested.' );
    end;

[注意:对于任何寻找电子邮件包的人,这是我从中获得的链接: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID: 255615160805 ]

我想用它来发送电子邮件,但是输入中的电子邮件地址p_to必须是动态输入的。也就是说,我不能像上面那样对它们进行硬编码。它们需要来自 SELECT 语句。我熟悉编写 select 语句,但我不知道如何将 selecte 语句的结果转换为可以包含在

p_to => mail_pkg.array( WHAT GOES HERE??? ),

代码。

任何人都知道我如何连接一个 SELECT 语句(它返回一些电子邮件地址)来使用这个包?

更新:

根据以下反馈,解决方案是:

create or replace procedure send_email ( 
  in_name IN varchar2
 )

AS

v_body clob;  
v_to_array mail_pkg.array := mail_pkg.array();
v_counter int := 1;

BEGIN

FOR r IN (SELECT person_email FROM email_table WHERE company_name=in_name) LOOP
  v_to_array.extend;
  v_to_array(v_counter) := r.person_email;
  v_counter := v_counter +1;
END LOOP;

-- send email

mail_pkg.send( p_sender_email => 'sam@company2.com',
               p_from => 'admin@company2.com',
               p_to => v_to_array,
               p_bcc => mail_pkg.array( 'tim@company3.com' ), 
               p_subject => 'the subject line goes here', 
               p_body => 'This is the body message.' );  

END send_email;
4

1 回答 1

3

您可以分两步完成。第一步是SELECT在循环中使用语句并填充数组;第二步是在调用中使用该数组mail_pkg.send,可能是这样的:

declare
  v_counter number := 1;
  v_to_arr mail_pkg.array := mail_pkg.array();
begin
  for r in Select email_address from table_of_email_addresses loop
    v_to_arr.extend;
    v_to_arr(v_counter) := r.email_address;
    v_counter := v_counter +1;
  end loop;

  mail_pkg.send(p_to => v_to_arr, ... );
  --note: this code may require some fine-tuning.
  ...
end ;

正如评论中所建议的,这里可能会使用批量收集,可能很简单:

declare
  v_to_arr mail_pkg.array := mail_pkg.array();
begin

  Select email_address
  bulk collect into v_to_arr
  from table_of_email_addresses;

  mail_pkg.send(p_to => v_to_arr, ... );
  --note: this code may require some fine-tuning.
  ...
end ;

我对批量操作有点生疏,您可能想自己阅读它们。

于 2013-05-08T15:29:47.173 回答