我有一个像
这样的XMLDeclare @BathData XML
SET @BathData='<Batch>
<Customers>
<Customer>
<CustomerId>1</CustomerId>
<Product>
<ProductId>10</ProductId>
<ProductId>11</ProductId>
</Product>
</Customer>
<Customer>
<CustomerId>2</CustomerId>
<Product>
<ProductId>22</ProductId>
<ProductId>23</ProductId>
<ProductId>25</ProductId>
</Product>
</Customer>
</Customers>
</Batch>'
我想要的结果如下
CusomerId ProductId
1 10
1 11
2 20
2 23
2 35
我正在使用以下方式对其进行排序
SELECT Finaldata.R.value('CustomerId[1]','int')
CustomerId,Finaldata.R.value('Product[1]','int') as ProductId
FROM @BathData.nodes('//Batch/Customers/Customer') as Finaldata (R)
但显然它没有快速响应会真的有帮助谢谢
答案 0 :(得分:0)
迭代ProductIds。您可以从两个级别(../../CustomerId
)获取客户:
SELECT Finaldata.R.value('(../../CustomerId)[1]','int') as CustomerId
, Finaldata.R.value('(.)[1]','int') as ProductId
FROM @BathData.nodes('/Batch/Customers/Customer/Product/ProductId')
as Finaldata (R)
Dot .
用作对当前节点的引用。