Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query and Reporting

Author  Topic 

abheja
Starting Member

1 Post

Posted - 2011-03-14 : 18:40:23
I am trying to set up an automated report that emails the recipients. It only works when the query returns a result. If no results qualify, the body of the email is BLANK. Please see the code below. If there is an alternate way to do this, I would be happy to learn. Thank you for your help.

DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML= N'<body >'+
N'<table border="1">' +
N'<tr><th>Full Name</th><th>DOB</th></tr>' +
CAST ( ( SELECT NAME,DOB FROM PERSON) WHERE P.DOB>’01/1/’2980’

FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'
</body></table>' ;
EXEC msdb.dbo.sp_send_dbmail
@profile_name = ‘Profile',
@recipients= ‘test@test.com’,
@subject = ' Weekly Report',
@body = @tableHTML,
@body_format = 'HTML' ;
   

- Advertisement -