来自varchar(max)列的SQL查询xml属性

时间:2017-05-04 10:52:50

标签: xml sql-server-2008-r2 xquery

我有一个名为Content的表,它有一个名为data的varchar(max)列。

此列中的数据采用XML格式(请注意,我没有设计数据库,所以我不知道为什么这个专栏不仅仅是XML格式,还有同一个数据库中的其他几个XML列... go figure ...),我想查询XML中的一个属性。

我已经复制了下面的XML,我想要查询的是p3:productId值。目前我有这个(从我在这里问到的另一个问题):

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance'
                          ,'http://www.w3.org/2001/XMLSchema-instance' AS p3)
SELECT CAST(Data AS XML).value(N'(/manifest/p3:productList/product/productId)[1]',N'nvarchar(max)') 
FROM Content

但是这只是为所有行返回NULL。

我确定它非常相似,但不能完全了解它,并且没有太多查询XML的经验(显然!)。

<manifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.w3.org/2001/XMLSchema-instance" 
      p3:manifestDateTime="2016-02-17T17:34:29.5925096Z" 
      p3:manifestVersion="1.1" p3:manifestId="Some_Data">
   <p3:productList>
    <p3:product p3:releaseDateTime="2016-02-17T17:34:29.5925096Z"         
    p3:installSeqId="2" p3:uninstallSeqId="2" p3:releaseNum="1" 
    p3:productType="doc" p3:productId="WEDREZ020RRRP0GGG001" p3:mfgCode="GIRE">

1 个答案:

答案 0 :(得分:1)

以下代码将提取xml的每个值(必须添加结束标记)。如果.nodes()中有许多不同的<p3:product>元素,则<p3:productList>调用将允许相同的查询:

DECLARE @xml XML=
'<manifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns="http://www.w3.org/2001/XMLSchema-instance" 
           p3:manifestDateTime="2016-02-17T17:34:29.5925096Z" 
           p3:manifestVersion="1.1" 
           p3:manifestId="Some_Data">
  <p3:productList>
    <p3:product p3:releaseDateTime="2016-02-17T17:34:29.5925096Z" 
                p3:installSeqId="2" 
                p3:uninstallSeqId="2" 
                p3:releaseNum="1" 
                p3:productType="doc" 
                p3:productId="WEDREZ020RRRP0GGG001" 
                p3:mfgCode="GIRE" />
  </p3:productList>
</manifest>';

- Thq查询

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance'
                          ,'http://www.w3.org/2001/XMLSchema-instance' AS p3)
SELECT @xml.value(N'(/manifest/@p3:manifestDateTime)[1]',N'datetime') AS manifestDateTime
      ,@xml.value(N'(/manifest/@p3:manifestVersion)[1]',N'nvarchar(max)') AS manifestVersion
      ,@xml.value(N'(/manifest/@p3:manifestId)[1]',N'nvarchar(max)') AS manifestId
      ,p.value(N'@p3:releaseDateTime',N'datetime') AS Product_releaseDateTime
      ,p.value(N'@p3:installSeqId',N'int') AS Product_installSeqId
      ,p.value(N'@p3:uninstallSeqId',N'int') AS Product_uninstallSeqId
      ,p.value(N'@p3:releaseNum',N'int') AS Product_releaseNum
      ,p.value(N'@p3:productType',N'nvarchar(max)') AS Product_productType
      ,p.value(N'@p3:productId',N'nvarchar(max)') AS Product_productId
      ,p.value(N'@p3:mfgCode',N'nvarchar(max)') AS Product_mfgCode
FROM @xml.nodes(N'/manifest/p3:productList/p3:product') AS A(p);

结果

+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+
| manifestDateTime        | manifestVersion | manifestId | Product_releaseDateTime | Product_installSeqId | Product_uninstallSeqId | Product_releaseNum | Product_productType | Product_productId    | Product_mfgCode |
+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+
| 2016-02-17 17:34:29.593 | 1.1             | Some_Data  | 2016-02-17 17:34:29.593 | 2                    | 2                      | 1                  | doc                 | WEDREZ020RRRP0GGG001 | GIRE            |
+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+