我有以下xquery Sql server 2008表。xquery返回for循環中的xml元素?
declare @tableName sysname = 'tableName'
declare @colNames xml = (select COLUMN_NAME from INFORMATION_SCHEMA.columns where TABLE_NAME = @tableName for xml path(''), elements)
select @colNames.query('<html><body>
<table>
<tr>
{
for $c in /COLUMN_NAME
return data($c)[1]
}
</tr>
</table>
</body></html>
')
但是,它返回以下xml。
<html>
<body>
<table>
<tr>col1col2col3col4col5</tr>
</table>
</body>
</html>
和預期的結果是
<html>
<body>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th></tr>
</table>
</body>
</html>
我試圖改變return data($c)[1]
到concat("<th>", data($c)[1], "</th>")
。然而,<
和>
被轉移到<
和>
?
的(http://stackoverflow.com/questions/7086393/create-html-table-with-sql-for [與FOR XML SQL創建HTML表]可能的複製-xml) – Shnugo 2016-09-14 14:42:44
嗨,如果這對你來說仍然很有趣,尤其是由於下面的註釋(*是否可以將表的數據附加到列標題下的xml中?*),我想指出你到一個[我剛剛回答的相關問題的回答](http://stackoverflow.com/a/39487565/5089204) – Shnugo 2016-09-14 14:44:38