如何使用xquery搜索具有多个相同类型节点的SQL XML列?

问题描述 投票:1回答:1

我在SQL中有一列保存XML数据。我想搜索它,但是目前没有得到想要的结果。 XML具有以下格式:

<item>
    <detail>
      <name>Item1</name>
      <price>Price1</price>
    </detail>
    <detail>
      <name>Item2</name>
      <price>Price2</price>
    </detail>
    <detail>
      <name>Item3</name>
      <price>Price3</price>
    </detail>
</item>

我想搜索表中具有“详细”节点且值名称为= Item2,价格= Price2的所有条目。我设法搜索了在父节点下分组的节点,如本例所示:

<item>
    <details>
      <detail>
        <name>Item1</name>
        <price>Price1</price>
     </detail>
     <detail>
        <name>Item1</name>
        <price>Price1</price>
     </detail>
     ...
   </details>
</item>

在这种情况下,我使用了以下sintax:

SELECT * FROM MyTable
WHERE XMLColumn.VALUE('(item/details/*/name)[1]', 'NVARCHAR(100)') = 'Item2'
AND XMLColumn.VALUE('(item/details/*/price)[1]', 'NVARCHAR(100)') = 'Price2'

但是如果父级“详细信息”节点不存在,则此操作无效。它仅搜索第一个“详细”节点,而跳过其余的节点。我在做什么错?

sql-server xml-parsing xmlnode xquery-sql
1个回答
0
投票
您应该改用exist()
© www.soinside.com 2019 - 2024. All rights reserved.