Solved

Extract XML Data from using TSQL

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

679 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