Background or font color in HTML is red
P粉038161873
P粉038161873 2023-09-03 09:18:19
0
1
783
<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>
P粉038161873
P粉038161873

reply all(1)
P粉550323338

Please try the following solutions.

It uses SQL Server XQuery functionality.

Additionally, it uses CSS to style the output XHTML.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (test VARCHAR(100) PRIMARY KEY, result INT);
INSERT @tbl (test, result) VALUES
('Bu', 57),
('Po', 5),
('Zu', 9);
-- DDL and sample data population, end

DECLARE @xhtmlBody XML
   , @body NVARCHAR(MAX)
   , @tableCaption VARCHAR(30) = 'Tests results report';

SET @xhtmlBody = (SELECT (
SELECT * FROM @tbl FOR XML PATH('row'), TYPE, ROOT('root'))
.query('<html><head>
            <meta charset="utf-8"/>
            (: including embedded CSS styling :)
            <style>
            table <![CDATA[ {border-collapse: collapse;  width: 300px;} ]]>
            th <![CDATA[ {background-color: #4CAF50; color: white;} ]]>
            th, td <![CDATA[ { text-align: left; padding: 8px;} ]]>
            tr:nth-child(even) <![CDATA[ {background-color: #f2f2f2;} ]]>
            td:nth-child(2)  {text-align: center;} 
            #red <![CDATA[ {background-color: red;} ]]>
         </style>
         </head>
         <body>
<table border="1">
   <caption><h2>{sql:variable("@tableCaption")}</h2></caption>
   <thead>
      <tr>
        <th>Test Name</th>
        <th>Formatted result</th>
      </tr>
   </thead>
   <tbody>
{
    for $row in /root/row
    return <tr>
            <td>{data($row/test)}</td>
            <td>
            {if (($row/result/text())[1] lt 10) then attribute id {"red"} else ()}
            {data($row/result)}            
         </td>
        </tr>
}
</tbody></table></body></html>'));

SELECT @xhtmlBody;
SET @body = TRY_CAST(@xhtmlBody AS NVARCHAR(MAX));

Output XHTML

<html>
  <head>
    <meta charset="utf-8" />
    <style>
            table  {border-collapse: collapse;  width: 300px;} 
            th  {background-color: #4CAF50; color: white;} 
            th, td  { text-align: left; padding: 8px;} 
            tr:nth-child(even)  {background-color: #f2f2f2;} 
            td:nth-child(2)  {text-align: center;} 
            #red  {background-color: red;} 
         </style>
  </head>
  <body>
    <table border="1">
      <caption>
        <h2>Tests results report</h2>
      </caption>
      <thead>
        <tr>
          <th>Test Name</th>
          <th>Formatted result</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Bu</td>
          <td>57</td>
        </tr>
        <tr>
          <td>Po</td>
          <td id="red">5</td>
        </tr>
        <tr>
          <td>Zu</td>
          <td id="red">9</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template