1

我的程序导致数据表包含 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              

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

谢谢

4

2 回答 2

2

游标可以解决您的问题:

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
于 2014-04-21T13:01:26.483 回答
1

已编辑

这应该工作

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
于 2014-04-21T09:56:10.600 回答