gpt4 book ai didi

sql - 使用 SQL Server 创建 XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:46:48 27 4
gpt4 key购买 nike

我有一个包含数据的表格:

itemID          itemLocation    quantity
-------------------------------------------------------
B008KZK44E COMMITED 1
B008KZK44E PRIME 1
B008KZK2LE COMMITED 1

我需要用这个节点结构生成一个 xml:

<inventoryItemData>
<itemID type="FAMILY">B008KZK2LE</itemID>
<availabilityDetail>
<itemQuantity>
<quantity unitOfMeasure="EA">1</quantity>
<itemLocation>COMMITED</itemLocation>
</itemQuantity>
</availabilityDetail>
</inventoryItemData>
<inventoryItemData>
<itemID type="FAMILY">B008KZK44E</itemID>
<availabilityDetail>
<itemQuantity>
<quantity unitOfMeasure="EA">1</quantity>
<itemLocation>COMMITED</itemLocation>
</itemQuantity>
</availabilityDetail>
<availabilityDetail>
<itemQuantity>
<quantity unitOfMeasure="EA">1</quantity>
<itemLocation>PRIME</itemLocation>
</itemQuantity>
</availabilityDetail>
</inventoryItemData>

我越接近这个:

SELECT 
'itemID' AS 'itemID/@type',
itemID AS 'itemID',
'' AS 'availabilityDetail',
'' AS 'availabilityDetail/itemQuantity',
'EA' AS 'availabilityDetail/itemQuantity/quantity/@unitOfMeasure',
quantity AS 'availabilityDetail/itemQuantity/quantity',
itemLocation AS 'availabilityDetail/itemQuantity/itemLocation'
FROM TABLE
FOR XML PATH ('inventoryItemData')

如果有任何解决方案,我将不胜感激。

谢谢。

最佳答案

select
'FAMILY' AS 'itemID/@type',
t1.itemID AS 'itemID',
(
select
'EA' AS 'itemQuantity/quantity/@unitOfMeasure',
t2.quantity AS 'itemQuantity/quantity',
t2.itemLocation AS 'itemQuantity/itemLocation'
from Table1 as t2
where t2.itemID = t1.itemID
for xml path('availabilityDetail'), type
)
from Table1 as t1
group by t1.itemID
for xml path ('inventoryItemData')

sql fiddle demo

关于sql - 使用 SQL Server 创建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com