本文介紹了查詢@xml 變量以獲取行集的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我有一個(gè)如下所示的 xml 數(shù)據(jù).
DECLARE @XmlContent XML設(shè)置@XmlContent ='<實(shí)體><實(shí)體類型=5"><item id="1"/><item id="2"/><item id="2"/></實(shí)體><實(shí)體類型=6"><item id="3"/><item id="4"/><item id="5"/></實(shí)體></實(shí)體>'
我想從中選擇數(shù)據(jù)并按以下格式插入表格 -
<前>------------類型 ID------------5 15 25 26 36 46 5有人可以幫我在 sql server 中為此編寫(xiě)查詢嗎?
解決方案
selectent.value('@type', 'int') 作為類型,row.value('@id', 'int') 作為 ID從@XmlContent.nodes('/Entities/Entity') foo(ent)交叉應(yīng)用 ent.nodes('item') bar(row)
I have an xml data that looks like below.
DECLARE @XmlContent XML
SET @XmlContent =
'<Entities>
<Entity type = "5">
<item id ="1"/>
<item id ="2"/>
<item id ="2"/>
</Entity>
<Entity type = "6">
<item id ="3"/>
<item id ="4"/>
<item id ="5"/>
</Entity>
</Entities>'
I want to select data from this and insert into a table in the following format -
------------ Type Id ------------ 5 1 5 2 5 2 6 3 6 4 6 5
Can some one help me to write query for this in sql server?
解決方案
select
ent.value('@type', 'int') as Type,
row.value('@id', 'int') as ID
from
@XmlContent.nodes('/Entities/Entity') foo(ent)
cross apply ent.nodes('item') bar(row)
這篇關(guān)于查詢@xml 變量以獲取行集的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!