我有以下查询允许我从指定列的xml行获取xml数据。
但是,产品可以有1个UPC孩子,或者很多。
如何更改查询以返回Product的所有现有子项?
DECLARE @MyXML XML
SET @MyXML =
(SELECT ProductsXML
FROM Products
WHERE ProductId= 1)
SELECT
a.b.value('Product[1]/UPC[1]/@upccode','DATE') as UPC,
a.b.value('Product[1]/UPC[1]/@dateadded','DATE') as DateAdded
FROM @MyXML.nodes('xml')a(b)
答案 0 :(得分:0)
DECLARE @MyXML XML;
SET @MyXML =
N'<xml>
<Product>
<UPC upccode="1237" dateadded="10/9/2012"/>
<UPC upccode="1236" dateadded="10/8/2012"/>
<UPC upccode="1235" dateadded="10/7/2012"/>
<UPC upccode="1234" dateadded="10/6/2012"/>
</Product>
</xml>';
SELECT
a.b.value('@upccode','int') as UPC,
a.b.value('@dateadded','date') as DateAdded
FROM
@MyXML.nodes('xml/Product/UPC')a(b);