我一直在爭取一段時間,似乎我很接近但並不完全在那裏。我有看起來像這樣的一個數據庫中的列:使用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而不轉換爲臨時表?