1

私は、データ テーブルに Emails,proposal_Type,count(MatchestoEmail) という 3 つの列が含まれているという結果の手順を持っています。ここで、Proposal_Type に基づいてこれらの数の一致があることを示す通知メールを「電子メール」に送信する必要があります。プロシージャの出力データは次のようになります。

Emails           Prop_Type   Matches

abc@gmail.com     1            3 

abc@gmail.com     2            4

def@gmail.com     3            2              

メールを受信者にし、残りの 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 に答える