在HTML中,背景或字体颜色为红色。

huangapple go评论79阅读模式
英文:

Background or font colour red in HTML

问题

I have the below code to send email notification to recipients and good. Source data is from a SQL Server query, I temporarily call "#Temp_Warning" table. My recipients want to highlight the value of T.FORMATTED_ENTRY in red in the email if it is less than 10. I have tried a lot but the values less than 10 shows unexpectedly the whole code "5" in the email instead of only number 5 in red.

This is the part of the code of a SQL Server Stored Procedure:

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

在HTML中,背景或字体颜色为红色。

英文:

I have the below code to send email notification to recipients and good. Source data is from a SQL Server query, I temporarily call "#Temp_Warning" table. My recipients want to highlight the value of T.FORMATTED_ENTRY in red in the email if it is less than 10. I have tried a lot but the values less than 10 shows unexpectedly the whole code &quot;span style=&quot;background-color:red;&quot;&gt;5&lt;/span&quot; in the email instead of only number 5 in red.

在HTML中,背景或字体颜色为红色。

This is the part of the code of a SQL Server Stored Procedure

IF (SELECT COUNT(*) FROM #Temp_Warning) &gt; 0 
	BEGIN

	--Format email content in HTML
	DECLARE @tableHTML NVARCHAR(MAX);
	SET @tableHTML =  
		N&#39;&lt;tr&gt;&#39; +
			N&#39;&lt;td&gt;&lt;b&gt;Test Name&lt;/b&gt;&lt;/td&gt;&#39; +
			N&#39;&lt;td&gt;&lt;b&gt;Formatted result&lt;/td&gt;&#39; +
		N&#39;&lt;/tr&gt;&#39; +
		CAST((
			SELECT
				td = T.REPORTED_NAME,&#39;&#39;,
				td = CASE WHEN T.FORMATTED_ENTRY &lt; 10 THEN N&#39;&lt;span style=&quot;background-color:red;&quot;&gt;&#39; + T.FORMATTED_ENTRY + N&#39;&lt;/span&gt;&#39; ELSE T.FORMATTED_ENTRY END,&#39;&#39;
			FROM #Temp_Warning T
			ORDER BY T.REPORTED_NAME 
			FOR XML PATH(&#39;tr&#39;), TYPE
		) AS NVARCHAR(MAX)) +
	END				

答案1

得分: 1

以下是翻译好的内容:

请尝试以下解决方案。

它使用了SQL Server XQuery 功能。

此外,它使用CSS来样式化输出的XHTML。

SQL

-- DDL和示例数据填充,开始
DECLARE @tbl TABLE (test VARCHAR(100) PRIMARY KEY, result INT);
INSERT @tbl (test, result) VALUES
('Bu', 57),
('Po', 5),
('Zu', 9);
-- DDL和示例数据填充,结束

DECLARE @xhtmlBody XML
, @body NVARCHAR(MAX)
, @tableCaption VARCHAR(30) = '测试结果报告';

SET @xhtmlBody = (SELECT (
SELECT * FROM @tbl FOR XML PATH('row'), TYPE, ROOT('root'))
.query('


{
for $row in /root/row
return

}

{sql:variable("@tableCaption")}

测试名称 格式化结果
{data($row/test)} {if (($row/result/text())[1] lt 10) then attribute id {"red"} else ()}
{data($row/result)}

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

输出的XHTML




测试结果报告

测试名称 格式化结果
Bu 57
Po 5
Zu 9


英文:

Please try the following solution.

It is using SQL Server XQuery powers.

Additionally, it is using CSS for styling output XHTML.

SQL

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

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

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

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

Output XHTML

&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;style&gt;
            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;} 
         &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;table border=&quot;1&quot;&gt;
      &lt;caption&gt;
        &lt;h2&gt;Tests results report&lt;/h2&gt;
      &lt;/caption&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Test Name&lt;/th&gt;
          &lt;th&gt;Formatted result&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Bu&lt;/td&gt;
          &lt;td&gt;57&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;Po&lt;/td&gt;
          &lt;td id=&quot;red&quot;&gt;5&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;Zu&lt;/td&gt;
          &lt;td id=&quot;red&quot;&gt;9&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年5月18日 08:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定