使用 XPath/XQuery 过滤 XML 列上的 SQL 查询 [英] Filter SQL queries on the XML column using XPath/XQuery

查看:29
本文介绍了使用 XPath/XQuery 过滤 XML 列上的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个 XML 列的表格.我想过滤掉 XML 中特定属性与字符串匹配的行,本质上是执行 WHERE 或 HAVING.

I'm having a table with one XML column. I'd like to filter out the rows where a specific attribute in the XML match a string, essentially doing a WHERE or HAVING.

桌子看起来像这样

| id | xml |

和 XML 类似的东西

And the XML something similar to

<xml>
  <info name="Foo">
    <data .../>
  </info>
<xml>

我想获取@name 属性与值匹配的所有 id.

I want to get all ids where the @name attribute matched a value.

我已经能够做到以下几点:

I have been able to do the following:

SELECT id, xml.query('data(/xml/info/@name)') as Value
FROM Table1
WHERE CAST(xml.query('data(/xml/info/@name)') as varchar(1024)) = @match

但它非常慢.

必须有更好的过滤查询输出的方法.

There must be a better way of filtering on the output of the query.

推荐答案

找到了.我应该使用 exist() 而不是使用 query().

Found it. Instead of using query() I should be using exist().

我的查询将是

SELECT id, xml.query('data(/xml/info/@name)') as Value
FROM Table1
WHERE xml.exist('/xml/info/[@name=sql:variable("@match")]') = 1

这篇关于使用 XPath/XQuery 过滤 XML 列上的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆