2

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">" ?

0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @mwigdahl for your example. This reasoning clears out my issue on how to dinamically read xml data types on SQL Server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.