1
我在尝试解析SQL Server中的一些送货细节(发送给我们ax XML)时遇到了一些困难。解析SQL Server中的XML数据问题
的XML结构如下:
<Parent>
<ShipmentNotice>
<VendorSKU>ABC123</VendorSKU>
<SerialNumbers>
<string>AAAABBBB</string>
<string>11112222</string>
<string>CCCCDDDD</string>
<string>33334444</string>
</SerialNumbers>
</VendorSKU>
</ShipmentNotice>
<ShipmentNotice>
<VendorSKU>123ABC</VendorSKU>
<SerialNumbers>
<string>EEEEFFFF</string>
<string>55556666</string>
<string>GGGGHHHH</string>
<string>77778888</string>
</SerialNumbers>
</VendorSKU>
</ShipmentNotice>
</Parent>
本质上正在发送的产品的每一种情况下具有每例如两个<string>
标签(AAAABBBB,11112222)。我期望做的是通过XML解析并根据我提供的VendorSKU为每个<string>
对创建一行。
我至今是:
SELECT
#ShippingRequests.[xmlcontent].value('(Parent/ShipmentNotice/VendorSKU)[1]', 'varchar(max)') AS VendorSKU,
#ShippingRequests.[xmlcontent].value('(Parent/ShipmentNotice/SerialNumbers/string)[1]', 'varchar(max)') AS ICCID,
#ShippingRequests.[xmlcontent].value('(Parent/ShipmentNotice/SerialNumbers/string)[2]', 'varchar(max)') AS IMEI
FROM
(SELECT
StoreID, CAST(REPLACE(CAST([RequestData] as NVARCHAR(MAX)), '<User=/>', '') AS XML) AS xmlcontent
FROM
#ShippingRequests) #ShippingRequests
--CROSS APPLY #ShippingRequests.xmlcontent.nodes('(Parent/ShipmentNotice/SerialNumbers/string)') AS t(c)
WHERE
#ShippingRequests.xmlcontent.value('(Parent/ShipmentNotice/VendorSKU)[1]', 'varchar(max)') = 'ABC123'
没有CROSS APPLY
运行这个,会出现两个问题:
- 我只得到了第一
<string>
对 - 一个结果只显示如果VendorSKU ABC123是第一个
<ShipmentNotice>
标签的一部分。如果VendorSKU ABC123第二个<ShipmentNotice>
在下一个XML中解析,那么什么都不显示。
与CROSS APPLY
一起运行,我得到了大量的重复。
无论VendorSKU在XML中显示的位置是什么,我如何获得此查询以运行所有<string>
对并运行?
什么SQL方言是您使用? Oracle,SQL Server,PostgreSQL?请在帖子中标记或提及。 – Parfait