I already used this method, but now I have to use some colors depending on the values. So, I have the following information in a table:
Material | Q1 | Q2
------
With the function mentioned in my comments you'd go like this:
This is the function
CREATE FUNCTION dbo.CreateHTMLTable
(
@SelectForXmlPathRowElementsXsinil XML
,@tblClass VARCHAR(100) --NULL to omit this class
,@thClass VARCHAR(100) --same
,@tbClass VARCHAR(100) --same
)
RETURNS XML
AS
BEGIN
RETURN
(
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 tbody
FOR XML PATH('table'),TYPE
)
END
GO
--Your test table
CREATE TABLE #tempo
(
q1 INT, q2 INT, name VARCHAR(10)
);
INSERT INTO #tempo (q1, q2, name)
VALUES (10, 5, 'low'), (10, 10, 'same'), (10, 20, 'high');
GO
--Inline CSS for easy formatting
DECLARE @inlineCSS XML=
N'';
--This is the actual query
SELECT @inlineCSS
,dbo.CreateHTMLTable
(
(
SELECT
CASE WHEN ISNULL(q1,0)>ISNULL(q2,0) THEN 'low'
ELSE CASE WHEN ISNULL(q2,0)>ISNULL(q1,0) THEN 'high'
ELSE 'same'
END
END AS [@class]
,name AS Material
,ISNULL(q1,0) AS [Q1]
,ISNULL(q2,0) AS [Q2]
FROM #tempo
FOR XML PATH('row'),ELEMENTS XSINIL),NULL,NULL,NULL
)
FOR XML PATH('body'),ROOT('html');
--Hint: Using classnames instead of the three --Clean-Up This is the result (click "Run" to see the output),NULL,NULL,NULL
allows to place general CSS classes to the , the
and the
.
GO
DROP TABLE #tempo
GO
DROP FUNCTION dbo.CreateHTMLTable;
Material
Q1
Q2
low
10
5
same
10
10
high
10
20