【问题标题】:How to send multiple emails from sql server database using sp_send_dbmail procedure如何使用 sp_send_dbmail 过程从 sql server 数据库发送多封电子邮件
【发布时间】:2014-04-21 09:45:00
【问题描述】:

我的程序导致数据表包含 3 列 Emails、proposal_Type、count(MatchestoEmail) 匹配基于proposal_type。 现在我必须向“电子邮件”发送通知邮件,说明他们有这么多基于 Proposal_Type 的匹配项。程序输出数据会是这样的。

Emails           Prop_Type   Matches

abc@gmail.com     1            3 

abc@gmail.com     2            4

def@gmail.com     3            2              

我希望邮件成为收件人,其余两列在电子邮件正文中添加一些附加文本。请帮帮我。

谢谢

【问题讨论】:

  • 我认为这是一个有教育意义的问题

标签: sql-server stored-procedures sp-send-dbmail


【解决方案1】:

光标可以解决您的问题:

DECLARE <yourvariables>
DECLARE emailCursor CURSOR FOR
SELECT emails, prop_type, matches FROM <yourtable>
OPEN emailCursor
FETCH NEXT FROM emailCursor INTO @email, @prop_type, @matches
WHILE @@FETCH_STATUS = 0
BEGIN
   SET @BODY = '<body text>' + @prop_type + '<more text>' + @matches
   EXEC msdb.dbo.sp_send_dbmail
   @recipients = @email,
   @subject = '<subject>',
   @body = @BODY
   FETCH NEXT FROM emailCursor INTO @email, @prop_type, @matches
END
CLOSE emailCursor
DEALLOCATE emailCursor

【讨论】:

    【解决方案2】:

    编辑

    这应该可以工作

    create proc [dbo].[SendProposalReport] as   
    declare rscursor cursor read_only
    for 
    select Emails, Prop_Type, count(Matches) as Matches  from proposals
    group by Emails, Prop_Type
    
        declare @Emails            nvarchar (100)
        declare @Prop_Type    int
        declare @Maches    int
    
    open rscursor
    fetch next from rscursor into @Emails,@Prop_Type,@Maches
    while @@fetch_status=0
        begin
    
             EXEC msdb.dbo.sp_send_dbmail
            @recipients = @Emails,
            @subject = 'Example Email',
            @body = 'write your message here',
            @profile_name = 'ExampleProfile',
    
            @attach_query_result_as_file = 1        
    
        fetch next from rscursor into @Emails,@Prop_Type,@Maches
    end
    close rscursor
    deallocate rscursor
    

    【讨论】:

    • 感谢您的回复,但这不是我要找的。​​span>
    • @Raj,你能给出更多解释吗,如果你提供一些你所拥有的代码示例会有所帮助
    • EXEC msdb.dbo.sp_send_dbmail收件人='结果数据表的电子邮件',主题='示例电子邮件',正文='数据表的其他两列',profile_name ='ExampleProfile'等
    • @recipients 必须来自我的程序结果数据,即 @recipients='Emails column' 和 @body='其他两列 Result'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多