3

I am new to SQL Server and T-SQL. How can I query out the information stored in this xml in SQL Server 2008 R2?

XML :

<smp:Root xmlns:smp="http://tempuri.org/smp.xsd" header="Test Title">
  <smp:Sections>
    <smp:G3 idnumber="01">
      <SectionHost>ABC</SectionHost>
    </smp:G3>
    <smp:G2 idnumber="01">
      <SectionHost>DEF</SectionHost>
    </smp:G2>
  </smp:Sections>
</smp:Root>
1
  • I added another link for the Xpath functions, they can help as well. Commented Jun 22, 2013 at 22:36

1 Answer 1

3

If you have the Xml stored in a Xml column just use the value method to shred the Xml.

Next time try and post some DDL, DML to show us what you have tried, your table structures etc.

But try this

WITH XMLNAMESPACES (Default 'http://tempuri.org/smp.xsd')
SELECT     
    a.value('@header', 'nvarchar(50)') as Header,
    b.value('local-name(.)', 'nvarchar(50)') as Sections,
    b.value('@idnumber' ,'int') as IdNumber,
    b.value('.' , 'nvarchar(20)') as Host

From ATable As x   

                Cross Apply x.AXmlColumn.nodes('Root') a(a) 
                               Cross Apply a.nodes('Sections/*') b(b)

Here are some useful links to get you started:

https://www.simple-talk.com/sql/learn-sql-server/the-xml-methods-in-sql-server/

http://blog.sqlauthority.com/2008/01/15/sql-server-what-is-dml-ddl-dcl-and-tcl-introduction-and-examples/

http://msdn.microsoft.com/en-us/library/ms189254.aspx

Sign up to request clarification or add additional context in comments.

Comments

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.