Reputation: 1755
I have the table: TestXml with 2 columns(pk , xCol). This is my table:
create table TestXml (
pk int primary key, xCol xml)
on [Primary]
insert into Store.dbo.TestXml(pk,xCol) values (1,'<docs><doc id="12"><section> Section 1</section></doc>
<doc id="123"><section> Section 1</section>
<section> Section 2</section>
</doc>
</docs>'
)
the xml value , that I was insert looks like this:
<docs>
<doc id="12">
<section> Section 1</section>
</doc>
<doc id="123">
<section> Section 1</section>
<section> Section 2</section>
</doc>
</docs>
How can I take "<section>
" element where "<doc id = "123">
" ?
Upvotes: 2
Views: 2618
Reputation: 16578
Try this:
SELECT c.d.value('(.)[1]', 'varchar (100)') AS Section
FROM Store.dbo.TestXml tx
CROSS APPLY tx.xCol.nodes('./docs/doc') AS a(b)
CROSS APPLY a.b.nodes('./section') AS c(d)
WHERE a.b.value('(@id)[1]', 'int') = 123
Upvotes: 3