SQL-分别从表中向多个用户发送相同的邮件

时间:2017-11-30 11:04:11

标签: sql sp-send-dbmail

我想使用 sp_send_dbmail 将相同的邮件发送到从表中分别获取的多个电子邮件ID。 如果我使用下面的查询,邮件将被发送给" To"部分。但我的要求是分别向他们发送邮件。

DECLARE @Receipientlist varchar(8000)  
SET @ReceipientList = STUFF((SELECT ';' + emailaddress FROM Your query here 
FOR XML PATH('')),1,1,'')


EXEC sp_send_dbmail  @profile_name= your email profile in db,  
@recipients= @Receipientlist,  
@subject='your subject here',  
@body='body message here'  

请提出一些解决方案。

1 个答案:

答案 0 :(得分:-1)

定义光标并尝试这样;

    declare @emailaddress nvarchar(250)

    DECLARE db_cursor CURSOR FOR  
    SELECT emailaddress FROM Your query here  

    OPEN db_cursor   
    FETCH NEXT FROM db_cursor INTO @emailaddress   

    WHILE @@FETCH_STATUS = 0   
    BEGIN   

            EXEC sp_send_dbmail  @profile_name= your email profile in db,  
            @recipients= @emailaddress,  
            @subject='your subject here',  
            @body='body message here' 

           FETCH NEXT FROM db_cursor INTO @emailaddress   
    END  

CLOSE db_cursor   
DEALLOCATE db_cursor