使用XML更改字体颜色并使其在sql中变为粗体

时间:2018-02-01 19:10:06

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

----- 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语句)中的“拒绝访问”?

2 个答案:

答案 0 :(得分:0)

参考: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>' ;

答案 1 :(得分:0)

使用我在评论中提到的功能,您可以这样做:

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>