我正在尝试编写一个存储过程,它将SQL查询的结果转换为HTML字符串,并将其显示在HTML表格中。
我遇到的问题是我使用FOR XML
获取逗号分隔的td值列表,然后尝试连接它,但是当我这样做时,它会丢失<
和{{ 1}}字符并将其替换为XML >
和<
- 我需要它将其显示为>
。
这是执行此操作的查询部分:
<>
然而,这就是它的回报:
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
如何让它显示为<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>
而不是<td> [Reasons_Code] </td>
答案 0 :(得分:0)
通过堆叠for xml
语句,您告诉SQL Server您要保留XML字符串中包含保留XML字符的值,例如:
<ActualTag>Your values that contain a < and > special character</ActualTag>
这显然是不可能的。仅仅因为您使用技巧来删除原本存在的标记,您仍然在处理XML 值。
要么改变你的字符串构建方法,要么改变select
最终XML变量中的value
:
declare @x xml = '<ActualTag>Your values that contain a < and > special character</ActualTag>';
select @x.value('.','nvarchar(max)')
输出:
Your values that contain a < and > special character