I am trying to write a stored procedure that will convert the results of a SQL query into a HTML string which will display them in a HTML table.
The problem I have is I am using FOR XML
to get a comma-separated list of td values and then trying to concatenate it, however when I do that, it loses the <
and >
characters and replaces them with the XML <
and >
- I need it to show me it as <>
.
here is the part of the query that does this:
declare C cursor for
select (convert(nvarchar(max),(SELECT "th/@width"='50', th = name
FROM tempdb.sys.columns isc
WHERE object_id = OBJECT_ID('tempdb.dbo.##tempcolumns')
for xml PATH(N''), TYPE)))
open C
declare @L varchar(max)
fetch next from C into @L
close C
deallocate C
DECLARE @html nvarchar(max), @table nvarchar(max), @test nvarchar(max), @html2 nvarchar(max)
SET @html =
N'<html> <head><title>TestHTML</title></head>' + CHAR(10) +
N'<body style="font-family: Arial;">' +
N'<h1>Companies2</h1></font>' +
'<table style="float: left; width:100%;">' +
N'<tr>' +
@L +
N'</tr>' + CHAR(10);
SELECT @table = CONVERT(nvarchar(max),(SELECT (
SELECT '<td>' + STUFF( ( SELECT td = ', ' + '[' + isc.name + ']'
FROM tempdb.sys.columns isc
WHERE isc.object_id = OBJECT_ID('tempdb.dbo.##tempcolumns') FOR XML PATH ('')), 1,5,'')) FROM ##temp FOR XML PATH(''), TYPE))
SET @html = @html + @table + CHAR(10) +
N'</table></body></html>'
SELECT @table
However, this is what it returns:
<td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td><td> [Reasons_Code]</td><td>, [Open]</td><td>, [Ordered]</td><td>, [Partially Ordered]</td><td>, [Lost]</td><td>, [Not Pursued]</td><td>, [Expired (Past 90 days)]</td><td>, [Pending]</td>
How do I get it to show for example <td> [Reasons_Code] </td>
and not <td> [Reasons_Code]</td>