Skip to content

Instantly share code, notes, and snippets.

@mbourgon
Created July 1, 2016 20:02
Show Gist options
  • Save mbourgon/6513552e5a7abab228d3b9be059d492d to your computer and use it in GitHub Desktop.
Save mbourgon/6513552e5a7abab228d3b9be059d492d to your computer and use it in GitHub Desktop.
Event Notifications - send HTML email when a ROUTE is changed/added/dropped
--check last 15 minutes to make sure nobody did a stupid.
DECLARE @tableHTML NVARCHAR(MAX);
WITH cte AS
(
SELECT PostTime, EventType, LoginName, ObjectName, ServerName FROM ENAudit_Events WHERE EventType LIKE '%route%'
AND PostTime > dateadd(minute, datediff(minute,0,GETDATE()) / 15 * 15, 0)
)
select @tableHTML = N'<H3>Event Notification ROUTE changes - make sure you meant to add/remove these from EN!' + '</H3>'
+ N'<table border="1">'
+ N'<th>PostTime</th>'
+ N'<th>EventType</th>'
+ N'<th>LoginName</th>'
+ N'<th>ServerName</th>'
+ N'<th>ObjectName</th>'
+ CAST(( SELECT
td = CONVERT(VARCHAR(50),RTRIM(LTRIM(ISNULL(PostTime,'')))) , ''
, td = CONVERT(VARCHAR(50), RTRIM(LTRIM(ISNULL(EventType,'')))) , ''
, td = CONVERT(VARCHAR(70),RTRIM(LTRIM(ISNULL(LoginName,'')))) , ''
, td = CONVERT(VARCHAR(70),RTRIM(LTRIM(ISNULL(ObjectName,'')))) , ''
, td = CONVERT(VARCHAR(50),RTRIM(LTRIM(ISNULL(ServerName,''))))
FROM cte
ORDER BY posttime
FOR XML PATH('tr') , TYPE ) AS NVARCHAR(MAX)) + N'</table>' ;
IF @tableHTML IS NOT NULL
begin
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'myprofile',
@recipients = '[email protected]', @subject = '[Event Notifications] Server added/removed - please verify',
@body = @tableHTML, @body_format = 'HTML' ;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment