Well, start by generating
valid HTML. You've got a stray
<
, and you've added
<th>
and
<td>
elements as direct children of the
<table>
, when they need to be nested in
<tr>
elements, preferably within the sematically-correct
<thead>
and
<tbody>
elements.
SET @tableHTML
= N'<h1>AutoPdf Status</h1>'
+ N'<table border="1">'
+ N'<thead><tr><th>Last Update</th><th>Last S48 Run</th><th>AutoPDF Status</th><th>Action Plan</th></tr></thead>'
+ N'<tbody>'
+ (
SELECT
N'<tr>'
+ N'<td>' + convert(varchar, [LastUpdate], 20) + N'</td>',
+ N'<td>' + convert(varchar, [LastS48Run], 20) + N'</td>',
+ N'<td>' + CASE
WHEN DATEDIFF(MINUTE, [LastUpdate], GETDATE()) > 1
AND DATEDIFF(MINUTE, [LastS48Run], GETDATE()) > 1 THEN 'Auto PDF Service is Hanging'
ELSE 'Normal'
END + N'</td>'
+ N'<td>' + CASE
WHEN DATEDIFF(MINUTE, [LastUpdate], GETDATE()) > 1
AND DATEDIFF(MINUTE, [LastS48Run], GETDATE()) > 1 THEN 'Restart AutoPDF Service on AL-DR-CORRESP Server'
ELSE 'Service is running'
END + N'</td>'
+ N'</tr>'
WHERE DATEDIFF(MINUTE, [LastUpdate], GETDATE()) > 1
AND DATEDIFF(MINUTE, [LastS48Run], GETDATE()) > 1
FOR XML PATH (''), TYPE
).value('.', 'nvarchar(max)')
+ N'</tbody>'
+ N'</table>';
If it still doesn't work, then you need to explain precisely what the problem is.