July 6, 2012 at 8:13 am
I have a procedure that is executed at last step of a job to report the previous steps' activity. When I manually executed the sp it sends the email and the body has the report in html format. But, when it's executed through the job the body is blank. Any ideas why?
DECLARE @tableHTML nvarchar(MAX), @RUN_DATE varchar(50), @BATCH_NUM int
SET @BATCH_NUM = (
SELECT *
FROM OPENQUERY(PROD, '
SELECT MAX(BATCH_NUM)
FROM SCHEMA.TABLE'
)
)
SET @RUN_DATE = LEFT((
SELECT *
FROM OPENQUERY(PROD, '
SELECT MAX(LOAD_DT)
FROM SCHEMA.TABLE'
)
), 10) + ' 20:00:00.000'
SET @tableHTML =
N'<H1>Header.</H1>' +
N'<table border="3" bordercolor="Blue">' +
N'<tr><th>BATCH_NUM</th><th>LOAD_DT</th>' +
N'<th>TABLE_NAME</th><th>ROW_COUNT</th></tr>' +
CAST((SELECT td = BATCH_NUM, ''
, td = @RUN_DATE, ''
, td = TABLE_NAME, ''
, td = ROW_COUNT, ''
FROM LINKEDSERVER.DB.dbo.TABLE
WHERE BATCH_NUM = @BATCH_NUM
For XML PATH('tr'), TYPE) as nvarchar(MAX)) +
N'</table>'
EXEC MSDB.dbo.sp_send_dbmail
@RECIPIENTS = 'email@work.com',
@SUBJECT = 'Successfull Balancing Issued Transaction Report Summary',
@BODY = @tableHTML,
@BODY_FORMAT = 'HTML',
@PROFILE_NAME = 'EmailProfile'
There is an exception to every rule, except this one...
July 6, 2012 at 8:52 am
print your @tableHTML variable before you get to the sp_sendmail.
is it null, maybe due to concatination? that would make your html body blank for sure.
check your one parameter and three columns for nulls?
BATCH_NUM
@RUN_DATE
TABLE_NAME
ROW_COUNT
Lowell
July 6, 2012 at 12:07 pm
You may have to make the query dynamic SQL (stored in a variable) and execute the variable in order to get the Body = @tableHTML part of it working correctly.
July 10, 2012 at 6:12 am
Thanks for your suggestions. Turns out that in although I had Database Mail XPs enabled, it also required SQL Mail XPs to be enabled.
There is an exception to every rule, except this one...
July 10, 2012 at 6:20 am
Thank you for letting us know. That will help with future questions.
Glad you got it working.
May 19, 2023 at 3:16 pm
Hi, I am facing the same issue - can you please let me know how you enabled SQL Mail XPs please? Thanks
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply