Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Script to send an email based on contents of a job step's output file

By Michael Lacy,

I frequently use the Advanced option on SQL Server Agent Job steps to declare an output file for the job's results. Sometimes, I then use a separate job step to email this output file to users. I have had a job, that I run nightly, that looks to see if a certain kind of error exists in some database tables. It is very rare for this error to occur, yet I was sending the job's output file, as an attachment, to my users every day. Of course this can cause recipients to become immune to the contents of the email (if you cry wolf too often.....). I decided to see if I could somehow check the contents of the outputted file to see if it contained a "(0 rows(s) affected)", statement. If it does contains a row that reads: "(0 rows(s) affected)", then we don't want to send an email. You could, of course, check for a different string value.

Total article views: 3596 | Views in the last 30 days: 4
 
Related Articles
FORUM

Scheduled Job: Email Output File within same step

A step emails the output file, but it may not be fully written as text still in the print buffer...

FORUM

outputting a sql database to an email /to windows

outputting a sql database to an email /to windows

BLOG

Building HTML Emails With SQL Server and XML

Building HTML Emails With SQL Server and XML I’ve written a lot of custom reports in my days that o...

FORUM

SP_WHO3 email

variable length column values in output cause format problems

Tags
email    
output file    
 
Contribute