开发者

Extract elements in xml to rows in select statement

开发者 https://www.devze.com 2023-01-28 14:26 出处:网络
I have got xml column in my sql server 2008 database. XML sample in each row of my table <document>

I have got xml column in my sql server 2008 database. XML sample in each row of my table

<document>
 <part1>
   <listite开发者_运维问答m>val1</listitem>
   <listitem>val2</listitem>
   <listitem>val3</listitem>
 </part1>
 <part2>
   <listitem>val4</listitem>
 </part2>
</document>

I would like to select all elements from all rows. From sample above I should get four rows with listitem value.

The answer is

select x.nd.value ('(.)[1]', 'varchar(250)') as ValuesFromXml
from TableWithXmlColumn t cross apply t.XmlContent.nodes (
'//listitem') x(nd);

Thanks for help


You can do it like this:

select Col.value('.', 'varchar(20)') 
from yourtable 
cross apply XmlColumn.nodes('//listitem') as NewTable(Col)
0

精彩评论

暂无评论...
验证码 换一张
取 消