【问题标题】:Changing font color and make it bold in sql by using XML使用 XML 在 sql 中更改字体颜色并使其变为粗体
【发布时间】:2018-07-12 05:04:37
【问题描述】:

-----HTML 表头去她--------

CAST (
select = td Table description
select = td Count Row
select = case when date(...Date variable goes here) >=18 then 'Access denied' 
else 'Access verified'

如何使用 xml 在 t-sql(cast 语句) 中更改字体并使其仅在“拒绝访问”时变为粗体?

【问题讨论】:

  • 这个问题非常模糊,您需要发布的不仅仅是上面的内容,其中包括似乎是 SQL 查询的想法,但并不完整。但是,要格式化 html 是由 SQL 生成的表格,就像使用任何 html 一样,请使用正确的标签。例如<b> 是粗体。
  • 这是一个 HTML 问题,而不是 XML。如果没有样式表或使用 XML 数据并对其进行格式化的东西,XML 本身就没有粗体的概念。如果要在 SQL 中执行此操作,则需要生成有效的 HTML。使用<b> 标签或<strong> 标签。
  • 字体颜色是显示问题。这不应该在数据库中完成。
  • 查看以下示例
  • @SeanLange 一般来说,您认为这是一个显示问题是正确的,我同意。不过,有时在数据库中这样做是有效的。例如,发送 HTML 格式的邮件时。

标签: sql-server xml tsql html-table xhtml


【解决方案1】:

参考:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=96536

刚刚检查并看到有人遇到同样的问题.. 我需要更改“客户”字体并将其设为粗体以用于以下示例

SET @tableHTML =
N'<H1>Status Brief Report</H1>' +
N'<b>This is what I have so far. I will start working on the formatting.<b>' 
+ 
 N'<table border="1">' +
N'<tr><th>Customer</th>' + 
N'<th>Bank</th>' +
N'<th>Safe</th>' +
N'<th>Enabled</th>' +  
N'<th>Poll Time</th>' +
N'<th>Data In CPR</th>' +
N'<th>Edge Matches CPR</th></tr>' +
CAST ( ( SELECT td = Customer, '',
td = Bank, '',
FROM #tempBrief
ORDER BY Customer, Bank, Safe
FOR XML PATH('tr'), TYPE 
) AS NVARCHAR(MAX) ) +
N'</table>' ;

【讨论】:

    【解决方案2】:

    使用我在您问题下方的评论中提到的功能,您可能会这样:

    USE master;
    GO
    CREATE DATABASE testDB; --test purpose
    GO
    USE testDB;
    GO
    CREATE FUNCTION dbo.CreateHTMLTable
    (
        @SelectForXmlRawElementsXsinil XML
       ,@tblClass VARCHAR(100)
       ,@thClass VARCHAR(100)
       ,@tbClass VARCHAR(100)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
    
    RETURN
    REPLACE(CAST(
    (
        SELECT @tblClass AS [@class]  
        ,@thClass AS [thead/@class]
        ,@SelectForXmlRawElementsXsinil.query('let $first:=/row[1]
                    return 
                    <tr> 
                    {
                    for $th in $first/*
                    return <th>{local-name($th)}</th>
                    }
                    </tr>') AS thead
        ,@tbClass AS [tbody/@class]
        ,@SelectForXmlRawElementsXsinil.query('for $tr in /row
                     return 
                     <tr>
                     {
                     for $td in $tr/*
                     return <td>{string($td)}</td>
                     }
                     </tr>') AS tbody
        FOR XML PATH('table'),TYPE
    ) AS NVARCHAR(MAX)),'_x0020_',' '); --blanks in column headers
    END
    GO
    

    --这是实际的查询

    SELECT 
    N'<H1>Status Brief Report</H1>' +
    N'<b>This is what I have so far. I will start working on the formatting.<b>' 
    + dbo.CreateHTMLTable(
    (
        SELECT 'The customer' AS [Customer]
              ,'The bank' AS [Bank]
              ,'Some safe' AS [Safe]
              ,'yes' AS [Enabled]
              ,CONVERT(VARCHAR(19),GETDATE(),121) AS [Poll Time]
              ,'yes' AS [DAta In CPR]
              ,'yes' AS [Edge Matches CPR]  
        FOR XML RAW, ELEMENTS XSINIL 
    )
    ,NULL,NULL,NULL);
    
    GO
    

    --清理

    USE master;
    GO
    DROP DATABASE testDB;
    GO
    

    结果

    <H1>Status Brief Report</H1>
    <b>This is what I have so far. I will start working on the formatting.<b>
    <table>
      <thead>
        <tr><th>Customer</th><th>Bank</th><th>Safe</th><th>Enabled</th><th>Poll Time</th><th>DAta In CPR</th><th>Edge Matches CPR</th></tr>
      </thead>
      <tbody>
        <tr><td>The customer</td><td>The bank</td><td>Some safe</td><td>yes</td><td>2018-02-02 09:52:09</td><td>yes</td><td>yes</td></tr>
      </tbody>
    </table>
    

    您可以将它与这里的 css-stlye 一起使用:

    <html>
    <style type="text/css" media="screen,print">
        table
        {
            color: black;
            font: arial;
            border: 1px solid black;
            border-collapse: collapse;
        }
        tr,th,td
        {
            border: 1px solid black;
        }
    </style>
    <body>
    <!--Your table here-->
    </body>
    </html>
    

    最后一页是这样的(点击“运行代码”)

    <html>
    <style type="text/css" media="screen,print">
        table
        {
            color: black;
    		font: arial;
    		border: 1px solid black;
    		border-collapse: collapse;
        }
    	tr,th,td
    	{
    		border: 1px solid black;
    	}
    </style>
    <body>
    <H1>Status Brief Report</H1><b>This is what I have so far. I will start working on the formatting.<b><table><thead><tr><th>Customer</th><th>Bank</th><th>Safe</th><th>Enabled</th><th>Poll Time</th><th>DAta In CPR</th><th>Edge Matches CPR</th></tr></thead><tbody><tr><td>The customer</td><td>The bank</td><td>Some safe</td><td>yes</td><td>2018-02-02 09:52:09</td><td>yes</td><td>yes</td></tr></tbody></table>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2012-10-13
      • 2020-07-07
      相关资源
      最近更新 更多