我有程序导致数据表包含3列电子邮件,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
我希望邮件成为收件人,剩余的两列将在电子邮件正文中添加一些附加文本。请帮助我。
谢谢
答案 0 :(得分: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
答案 1 :(得分: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