1

我有一个可以从 xml 返回结果的查询:

declare @xml xml

select @xml = data from files where id = 1234

select
    children.p.value('./speed[1]','float')
from @xml.nodes('root/children') as children(p)
where
    children.p.value('./name[1]','nvarchar(max)') = 'something'

这在我的情况下返回一个值,例如3141

但是,我想从多个 XML 中进行多次选择。

我可以选择 xml 数据为

select id, cast(data as xml) as xml
from files
where id in (1005,51,968,991,992,993,969,970) --for example

我想一定有某种 JOIN 将应用我的表达式并为xml表中的每个变量返回一个项目,但我不确定如何。

4

1 回答 1

2

使用适用:

select
    f.id, children.p.value('./speed[1]','float')
from files as f
    outer apply (select cast(f.data as xml) as xml) as x
    outer apply x.xml.nodes('root/children') as children(p)
where
     f.id in (1005,51,968,991,992,993,969,970) and
     children.p.value('./name[1]','nvarchar(max)') = 'something'
于 2013-10-25T14:00:03.907 回答