Solved

Extract XML Data from using TSQL

Posted on 2016-11-14
5
28 Views
Last Modified: 2016-11-15
I have a table with this structure on a SQL Server 2008 DB:

Create Table Test (
	Id int,
	Attributes XML
)

Open in new window


and with this data:

1, '<Attributes><ProductAttribute ID="1"><ProductAttributeValue><Value>1</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="2"><ProductAttributeValue><Value>2</Value></ProductAttributeValue></ProductAttribute></Attributes>'
2, '<Attributes><ProductAttribute ID="1"><ProductAttributeValue><Value>1</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="2"><ProductAttributeValue><Value>3</Value></ProductAttributeValue></ProductAttribute></Attributes>'

Open in new window


For readability, here's what the first XML column data looks like:
<Attributes>
	<ProductAttribute ID="1">
		<ProductAttributeValue>
			<Value>1</Value>
		</ProductAttributeValue>
	</ProductAttribute>
	<ProductAttribute ID="2">
		<ProductAttributeValue>
			<Value>2</Value>
		</ProductAttributeValue>
	</ProductAttribute>
</Attributes>

Open in new window


I'm trying to extract the data form the table:

Id, Attribute

1, 1
1, 2
2, 1
2, 3      


I've tried following this post  to flatten the xml and extract the data I want but cannot get my head around it.  Is this the right path?
0
Comment
Question by:canuckconsulting
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
E.g.
DECLARE @Test TABLE ( Id INT, Attributes XML );

INSERT  INTO @Test
VALUES  ( 1,
          N'<Attributes><ProductAttribute ID="1"><ProductAttributeValue><Value>1</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="2"><ProductAttributeValue><Value>2</Value></ProductAttributeValue></ProductAttribute></Attributes>' ),
        ( 2,
          N'<Attributes><ProductAttribute ID="3"><ProductAttributeValue><Value>1</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="2"><ProductAttributeValue><Value>3</Value></ProductAttributeValue></ProductAttribute></Attributes>' );

SELECT  T.Id ,
        ProductAttributes.value('@ID', 'INT') AS ProductAttributeID ,
        ProductAttributeValues.value('.', 'INT') AS ProductAttributeValue
FROM    @Test T
        OUTER APPLY Attributes.nodes('/Attributes/ProductAttribute') AS A ( ProductAttributes )
        OUTER APPLY ProductAttributes.nodes('ProductAttributeValue/Value') AS B ( ProductAttributeValues );

Open in new window

1
 

Author Comment

by:canuckconsulting
Comment Utility
Thank you!  I don't quite understand how the bits underlinedbelow work.  Can you explain how these select the data?


        ProductAttributes.value('@ID', 'INT') AS ProductAttributeID ,
        ProductAttributeValues.value('.', 'INT') AS ProductAttributeValue
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
You can read this from below URL. <<nopts..>>

https://msdn.microsoft.com/en-IN/library/ms188282.aspx

Hope it helps !!
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Extracting (parsing) XML is based on the XPath language.

The first expression ProductAttributes.value('@ID', 'INT') extracts from the current node the attribute (the @ means read attribute values instead of elements) named ID. The current node is a ProductAttribute node. The result is converted to an integer.

The second expression ProductAttributeValues.value('.', 'INT') returns the text of the current node, where the current node is /Attributes/ProductAttribute/ProductAttributeValue/Value - the value node. The dot is just the syntax for getting the element content. Which is also converted to integer.
0
 

Author Closing Comment

by:canuckconsulting
Comment Utility
Thank you for your help and helpful explanation re xpath.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now