【发布时间】:2015-08-14 23:15:28
【问题描述】:
我在尝试在 SQL Server 中解析一些运输详细信息(发送给我们 ax 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 是下一个要解析的 XML 中的第二个<ShipmentNotice>,则不会显示任何内容。
使用CROSS APPLY 运行此程序,我得到了大量重复项。
如何让此查询针对所有 <string> 对运行,并且无论 VendorSKU 在 XML 中显示的位置如何都运行?
【问题讨论】:
-
您使用的是什么 SQL 方言?甲骨文、SQL Server、PostgreSQL?请在帖子中标记或提及。
标签: sql sql-server xml xquery