[英]Nest nodes in XML generated from SQL
我正在嘗試生成一個如下所示的XML文件。
<products>
<product>
<sku>12345</sku>
<attributes>
<attribute>
<name>AttributeName</name>
<row>
<name>ProdName</name>
<value>someProduct</value>
</row>
</attribute>
<attribute>
<name>AttributeName</name>
<row>
<name>color</name>
<value>Blue</value>
</row>
</attribute>
</attributes>
</product>
</products>
通過以下SQL查詢,我可以生成非常接近的內容,但需要對其進行微調。
bcp "SELECT RTRIM(LTRIM(sku)) as sku,
(SELECT 'AttributeName' AS [name],'ProdName' AS [row/name],ProdName AS [row/value] FOR XML PATH ('attribute'), Type),
(SELECT 'AttributeName' AS [name],'color' AS [row/name], color AS [row/value] FOR XML PATH ('attribute'), Type)
FROM Tbl_Product WHERE (SKU = 12345) FOR XML PATH ('product'), ROOT('products'),Type" queryout "..\Desktop\sample.xml" -c -T
<products>
<product>
<sku>12345</sku>
<!-- need <attributes> here -->
<attribute>
<name>AttributeName</name>
<row>
<name>ProdName</name>
<value>someProduct</value>
</row>
</attribute>
<attribute>
<name>AttributeName</name>
<row>
<name>color</name>
<value>Blue</value>
</row>
</attribute>
<!-- end <attributes> here-->
</product>
</products>
有沒有一種方法可以將所有<attribute>
節點嵌套在<attributes>
。
提前致謝!
我不知道SQL中的字符串連接(+)。 我花了一個多星期的時間才弄清這件事。 這是解決我的問題的方法。 將來可能會幫助其他人。
bcp "SELECT RTRIM(LTRIM(sku)) as sku, (SELECT 'AttributeName' AS [attribute/name],
'ProdName' AS [attribute/row/name],
ProdName AS [attribute/row/value],
+'',
'AttributeName' AS [attribute/name],
'color' AS [attribute/row/name],
color AS [attribute/row/value]
FROM Tbl_Product Where (SKU = TP.SKU)
FOR XML PATH (''), ROOT('attributes'), Type)
FROM Tbl_Product TP WHERE (SKU = 12345)
FOR XML PATH ('product'), ROOT('products'),Type" queryout "..\Desktop\sample.xml" -c -T
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.