Background or font color in HTML is red
P粉038161873
2023-09-03 09:18:19
<p>I have the following code to send email notifications to recipients and it works great. The source data comes from a SQL Server query, which I temporarily call the "#Temp_Warning" table. If the value of T.FORMATTED_ENTRY is less than 10, my recipient would like the value to be highlighted in red in the email. I've tried a lot, but values less than 10 unexpectedly show the entire code<code>"span style="background-color :red;">5</span"</code> in the email, Instead of just the number 5 in red. </p>
<p>This is part of the code for a SQL Server stored procedure</p>
<pre class="brush:sql;toolbar:false;">IF (SELECT COUNT(*) FROM #Temp_Warning) > 0
BEGIN
--Format email content in HTML
DECLARE @tableHTML NVARCHAR(MAX);
SET @tableHTML =
N'<tr>'
N'<td><b>Test Name</b></td>'
N'<td><b>Formatted result</td>'
N'</tr>'
CAST((
SELECT
td = T.REPORTED_NAME,'',
td = CASE WHEN T.FORMATTED_ENTRY < 10 THEN N'<span style="background-color:red;">' T.FORMATTED_ENTRY N'</span>' ELSE T.FORMATTED_ENTRY END,''
FROM #Temp_Warning T
ORDER BY T.REPORTED_NAME
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX))
END
</pre></p>
Please try the following solutions.
It uses SQL Server XQuery functionality.
Additionally, it uses CSS to style the output XHTML.
SQL
Output XHTML