本文介紹了從 Sql Server 中的 xml 中的最后一個(gè)獲取第 n 個(gè)元素的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號(hào)..
請(qǐng)考慮這個(gè) XML:
<Employees>
<Person>
<ID>1000</ID>
<Name>Nima</Name>
<LName>Agha</LName>
</Person>
<Person>
<ID>1001</ID>
<Name>Ligha</Name>
<LName>Ligha</LName>
</Person>
<Person>
<ID>1002</ID>
<Name>Jigha</Name>
<LName>Jigha</LName>
</Person>
<Person>
<ID>1003</ID>
<Name>Aba</Name>
<LName>Aba</LName>
</Person>
</Employees>
我想寫一個(gè)函數(shù)來獲取一個(gè)數(shù)字,然后我得到第n個(gè)Person
元素和Name
.例如,如果 0 傳遞給我的函數(shù),我返回 Aba
,如果 1 傳遞給我的函數(shù),我返回 Jigha
.
I want to write a function that gets a number, and then I get nth Person
element, and Name
. For example if 0 pass to my function I return Aba
, if 1 pass to my function I return Jigha
.
推薦答案
這應(yīng)該有效.將 @index
變量的值設(shè)置為要查找的記錄的編號(hào),相對(duì)于列表的末尾:
This should work. Set the value of the @index
variable as the number of the record to find, relative to the end of the list:
declare @index int = 1
declare @xml xml = '<Employees>
<Person>
<ID>1000</ID>
<Name>Nima</Name>
<LName>Agha</LName>
</Person>
<Person>
<ID>1001</ID>
<Name>Ligha</Name>
<LName>Ligha</LName>
</Person>
<Person>
<ID>1002</ID>
<Name>Jigha</Name>
<LName>Jigha</LName>
</Person>
<Person>
<ID>1003</ID>
<Name>Aba</Name>
<LName>Aba</LName>
</Person>
</Employees>'
select t2.person.value('(Name/text())[1]','varchar(50)')
from @xml.nodes('Employees/Person[position()=(last()-sql:variable("@index"))]') as t2(person)
這篇關(guān)于從 Sql Server 中的 xml 中的最后一個(gè)獲取第 n 個(gè)元素的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!