把select 结果集转换为xml 格式的
OrderNumberItemNumberQtyRateQtyPicked
00001A0011011.250
00001A0022015.000
00001A0033023.750
转换后的结果是下面的
用 for xml path 和auto 都搞不定。。。
<orderInfo>
<itemInfo>
<OrderNumber>00001</OrderNumber>
<ItemNumber>A001</ItemNumber>
<Qty>10</Qty>
</itemInfo>
<itemInfo>
<OrderNumber>00001</OrderNumber>
<ItemNumber>A002</ItemNumber>
<Qty>20</Qty>
</itemInfo>
<itemInfo>
<OrderNumber>00001</OrderNumber>
<ItemNumber>A003</ItemNumber>
<Qty>30</Qty>
</itemInfo>
</orderInfo>
[最优解释]
--> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([OrderNumber] VARCHAR(5),[ItemNumber] VARCHAR(4),[Qty] INT,[Rate] NUMERIC(4,2),[QtyPicked] INT)
INSERT #tb
SELECT '00001','A001',10,11.25,0 UNION ALL
SELECT '00001','A002',20,15.00,0 UNION ALL
SELECT '00001','A003',30,23.75,0
GO
--> 测试语句:
SELECT
OrderNumber,
ItemNumber,
Qty
FROM #tb itemInfo
FOR XML AUTO, TYPE, ELEMENTS, ROOT('orderInfo')