Solved

Extract XML Data from using TSQL

Posted on 2016-11-14
5
59 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 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 41886145
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
ID: 41886175
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 28

Expert Comment

by:Pawan Kumar
ID: 41886242
You can read this from below URL. <<nopts..>>

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

Hope it helps !!
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41886275
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
ID: 41888648
Thank you for your help and helpful explanation re xpath.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Re-appearing SQL Server Agent jobs 7 28
SQL Server Configuration Manager WMI Error 11 16
Dynamic SQL select query 4 36
SQL Quer 4 21
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

791 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