本文介紹了從 Sql Server 中的 xml 中的最后一個獲取第 n 個元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
請考慮這個 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>
我想寫一個函數來獲取一個數字,然后我得到第n個Person
元素和Name
.例如,如果 0 傳遞給我的函數,我返回 Aba
,如果 1 傳遞給我的函數,我返回 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
.
推薦答案
這應該有效.將 @index
變量的值設置為要查找的記錄的編號,相對于列表的末尾:
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)
這篇關于從 Sql Server 中的 xml 中的最后一個獲取第 n 個元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!