【问题标题】:Using XML.Query for filtering multiple values使用 XML.Query 过滤多个值
【发布时间】:2009-08-13 07:46:34
【问题描述】:

我的表中有 XML 列,其中可以包含如下 2 条记录的 XML:

记录1,假设记录ID为1,XML内容为

<content>
 <node1 >10</node1>
 <node2 >20</node2>
 <node3 >30</node3>
 <node4 >40</node4>
 <node5 >50</node5> 
</content>

记录2,假设记录ID为2,XML内容为

<content>
 <node_name_1 >10</node_name_1>
 <hello >20</hello>
</content>

我想知道内部节点值为 10 的记录的 ID。

declare @ids nvarchar(500)
set @ids = '10';
select id from tablename
 where
 convert(nvarchar(max),xmlfieldname.query('data(/content/* = ( sql:variable("@ids") )  )') ) = 'true'

这是完美的工作,但我的问题是当我想为内部节点使用多个值时。我想知道内部值为 10 或 20 的记录的 ID。此查询完美运行:

 select id from tablename
 where
 convert(nvarchar(max),xmlfieldname.query('data(/content/* = ( 10,20 )  )') ) = 'true'

但是当我使用 sql:variable 时,什么都没有返回!

declare @ids nvarchar(500)
set @ids = '10,20';
select id from tablename
 where
 convert(nvarchar(max),xmlfieldname.query('data(/content/* = ( sql:variable("@ids") )  )') ) = 'true'

如何使用 Select * from mytable where fieldname in ('123','456') in XML 之类的东西?

【问题讨论】:

    标签: sql-server xml


    【解决方案1】:

    您要求它查找该值具有“10,20”的条目,就像您在说“where fieldname in ('10,20')”一样。

    这是一个古老的拆分问题......

    所以...试试:

    declare @qry nvarchar(max);
    select @qry = 'select id from tablename where convert(nvarchar(max),xmlfieldname.query(''data(/content/* = ( ' + @ids + ' )  )'') ) = ''true''';
    exec sp_executesql @qry
    

    罗伯

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多