【发布时间】:2012-01-12 23:10:43
【问题描述】:
我已经为此奋斗了一段时间,似乎我已经很接近了,但并不完全在那里。我在数据库中有一个如下所示的列:
<document>
<items>
<item name="one">one is the first number</item>
<item name="two">two is the second number</item>
</items>
</document>
在本例中,我需要查询并返回“二是第二个数字”。我也想在不创建临时表的情况下执行此操作。目前我有:
create table #test (item1 xml)
insert into #test (item1)
values ('<document> <items> <item name="one">one is the first number</item> <item name="two">two is the second number</item> </items> </document>')
select item1.value('(/document/items/item)[2]', 'nvarchar(max)') from #test
select item1.query('/document/items/item[@name="two"]') from #test
第一个选择返回正确的值,但我需要知道它是第二个“索引” 第二个返回我想要的,但它返回整个节点两个..
我错过了什么?而且,有没有一种简单的方法可以在不转换为临时表的情况下使用 XML?
【问题讨论】: