I'm trying to send an email using sp_send_dbmail
in Sql Server 2005. I have both body text and a query being sent as an attachment.
Sometimes, however, the query will return an empty dataset.
Is there any way for me to test the results of the dataset before I send out the email, and if it has no results, not send it as an attachment.
I was thinking to maybe run the query before I send the email, and to test the results that way. Then, I'd have an if-else
as follows:
if @@rowcount >0
EXEC msdb.dbo.sp_send_dbmail @recipients=@recipients_list,
@subject = @subject,
@body = @body_text, @body_format = 'HTML',
@query = @query,
@attach_query_result_as_file = 1,
@query_result_width = 4000,
@query_attachment_filename = 'Details.txt'
else
EXEC msdb.dbo.sp_send_dbmail @recipients=@recipients_list,
@subject = @subject,
@body = @body_text, @body_format = 'HTML'
But I don't think this is an efficient way to solve the problem.
Any suggestions? TIA!!