Agent Jobs

Use Distribution Groups For Database Mail

Use Distribution Groups For Database Mail

You’ll find many examples online on how to configure SQL Database Mail. These work great to demonstrate the process, but some might get confused on what to actually use for the ‘recipients’. It’s better to simply use a distribution group. If you need to add more people to the email notifications simply add them to the distribution group. Otherwise; you would end up hardcoding email addresses in and out of your notification steps within your jobs. Not a good idea, and completely inefficient. Have more than one ‘type’ of alerts going on? Create more distribution groups and apply them accordingly. Do not mettle around directly in your notification logic if you can help it.

Sample notification logic:

declare @server_name varchar(255)
declare @job_name varchar(255)
declare @message_subject varchar(255)
declare @message_body varchar(max)

set @server_name = (select @@servername )
set @job_name = (select name from msdb.dbo.sysjobs where @jobid = convert(uniqueidentifier, $(escape_none(jobid))))
set @message_subject = (select 'Job Failure on Server: ' + @server_name + ' Job: ' + @job_name
set @message_body = @message_subject + char(10) + char(10) + 'The Job failed at step (step name):'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLDatabaseMailProfile'
, <em>@recipients = 'AnotherEmailAddress@mydomain.com', 'AnotherEmailAddress', 'AnotherEmailAddress'</em> <img src="https://mikesdatawork.files.wordpress.com/2015/06/image002.png" alt="" width="14" height="14" class="alignnone size-full wp-image-568" /> NOT efficient.
, @recipients = 'sqladminalerts@mydomain.com' <img src="https://mikesdatawork.files.wordpress.com/2015/06/image002.png" alt="" width="14" height="14" class="alignnone size-full wp-image-568" /> More efficient. Use distribution groups.
, @subject = @message_subject
, @body = @message_body

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s