这是一个通用解决方案,其中包含FUNCTION在……上面XML-基地使用FLWOR
它会改变任何SELECT到XHTML表中。
它的工作(测试)与2008 R2+,但我非常肯定,这将在2008年,甚至可能是在2005年。如果有人想核实这一点,请留下评论。THX
下面的函数替换了我以前提供的所有函数(如果需要,请参阅前一个版本)CREATE FUNCTION dbo.CreateHTMLTable(
@SelectForXmlPathRowElementsXsinil XML ,@tblClass VARCHAR(100) --NULL to omit this class
,@thClass VARCHAR(100) --same ,@tbClass VARCHAR(100) --same)RETURNS XML
ASBEGINRETURN
(
SELECT @tblClass AS [@class]
,@thClass AS [thead/@class]
,@SelectForXmlPathRowElementsXsinil.query(
N'let $first:=/row[1]
return
{
for $th in $first/*
return
{if(not(empty($th/@caption))) then xs:string($th/@caption) else local-name($th)}}
') AS thead ,@tbClass AS [tbody/@class],@SelectForXmlPathRowElementsXsinil.query(
N'for $tr in /row
return
{$tr/@class}{
for $td in $tr/*
return
if(empty($td/@link))
then
{$td/@class}{string($td)}else
{$td/@class} {string($td)}}
') AS tbodyFOR XML PATH('table'),TYPE) ENDGO
最简单的呼叫
一个具有一些值的模拟表。DECLARE @tbl TABLE(ID INT, [Message] VARCHAR(100));INSERT INTO @tbl VALUES (1,'Value 1'),(2,'Value 2');
-呼叫必须包括SELECT ... FOR XML妄想症!
-点击运行片段看看结果!SELECT dbo.CreateHTMLTable(
(SELECT * FROM @tbl FOR XML PATH('row'),ELEMENTS XSINIL)
,NULL,NULL,NULL);
IDMessage
1Value 12Value 2如果需要带有空白的标头
如果您的表包含列的名称为空白。,或者如果要手动设置列的标题(多种语言支持!),或者如果要替换CamelCaseName通过输出标题,可以将其作为属性传递:DECLARE @tbl2 TABLE(ID INT, [With Blank] VARCHAR(100));INSERT INTO @tbl2 VALUES (1,'Value 1'),(2,'Value 2');
SELECT dbo.CreateHTMLTable(
(
SELECT ID ,'The new name' AS [SomeOtherName/@caption] --set a caption
,[With Blank] AS [SomeOtherName]
FROM @tbl2 FOR XML PATH('row'),ELEMENTS XSINIL )
,NULL,NULL,NULL);
IDThe new name
1Value 12Value 2完整的css-支持和超链接
您可以使用属性传递链接或基于行的类,甚至是基于值的类来标记CSS样式的列甚至单元格。
--a mock-up table with a row based condition and hyper-links
DECLARE @tbl3 TABLE(ID INT, [With blank] VARCHAR(100),Link VARCHAR(MAX),ShouldNotBeNull INT);
INSERT INTO @tbl3 VALUES
(1,'NoWarning',NULL,1)
,(2,'No Warning too','http://www.Link2.com',2)
,(3,'Warning','http://www.Link3.com',3)
,(4,NULL,NULL,NULL)
,(5,'Warning',NULL,5)
,(6,'One more warning','http://www.Link6.com',6);
--The query adds an attribute Link to an element (NULL if not defined)
SELECT dbo.CreateHTMLTable
(
(
SELECT
CASE WHEN LEFT([With blank],2) != 'No' THEN 'warning' ELSE NULL END AS [@class] --The first @class is the
-class,ID
,'center' AS [Dummy/@class] --a class within TestText (appeary always)
,Link AS [Dummy/@link] --a mark to pop up as link
,'New caption' AS [Dummy/@caption] --a different caption
,[With blank] AS [Dummy] --blanks in the column's name must be tricked away...
,CASE WHEN ShouldNotBeNull IS NULL THEN 'MarkRed' END AS [ShouldNotBeNull/@class] --a class within ShouldNotBeNull (appears only if needed)
,'Should not be null' AS [ShouldNotBeNull/@caption] --a caption for a CamelCase-ColumnName
,ShouldNotBeNull
FROM @tbl3 FOR XML PATH('row'),ELEMENTS XSINIL),'testTbl','testTh','testTb'
);
.center
{
text-align: center;
}
.warning
{
color: red;
}
.MarkRed
{
background-color: red;
}
table,th
{
border: 1px solid black;
}
IDNew captionShould not be null
1NoWarning12 23 345Warning56 6作为一个可能的增强,您可能会传递一个单排脚将聚合值作为附加参数,并将其追加为