我一直在争取一段时间,似乎我很接近但并不完全在那里。我有看起来像这样的一个数据库中的列:使用TSQL解析/查询XML
<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而不转换为临时表?