Sending Email in SQL Server 2008 R2

后端 未结 1 823
别那么骄傲
别那么骄傲 2021-01-28 13:46

I am tasked with a feature to send e-mail reminders to employees in my company if they haven\'t completed an attestation form via an intranet Web application.

I was thin

相关标签:
1条回答
  • 2021-01-28 14:49

    I reckon Inside your procedure you could create a Temp table/Table Variable and populate it with the emails you want to send email to.

    Once you have all the emails in a table then you could concatenate the email addresses with ; and store it to a variable and pass that variable as a parameter to @recipients parameter of msdb.dbo.sp_send_dbmail proc.

    Something like this...

    Say you have populated a table variable called Emails inside your procedure

    DECLARE @Emails TABLE(Email NVARCHAR(1000))
    INSERT INTO @Emails VALUES
    ('aaa@aaa.com'),('bbb@aaa.com'),('ccc@aaa.com')  --<-- Three emails you want to send email
    

    Concatenation of emails

    DECLARE @Email_List NVARCHAR(MAX);   --<-- Variable to store emails List
    
    SELECT @Email_List = STUFF((SELECT ';' + Email [text()]
                                FROM @Emails
                                FOR XML PATH(''),TYPE)
                                .value('.','NVARCHAR(MAX)'),1,1, '')                   
    FROM @Emails e 
    
    -- Test SELECT @Email_List
    -- RESULT:  aaa@aaa.com;bbb@aaa.com;ccc@aaa.com
    

    Now pass this variable to @recipients parameter

    EXECUTE msdb.dbo.sp_send_dbmail  @profile_name = 'ProfileName'  
                                   , @recipients   = @Email_List
                                   , @subject      = 'Some_Subject'
    
    0 讨论(0)
提交回复
热议问题