Solved

Get data as a datatable from XML column

Posted on 2011-09-13
14
417 Views
Last Modified: 2012-05-12
Hi!

Need some help to get data from an xml column into a datatable.

This is the data in the xml column
<RMS><V>1.45</V><V>4.35</V><V>3.65</V><</RMS>
I need this to be returned as a datatable

RMS
1.45
4.35
3.65

I got help with this in another post but the problem I now got is how to solve this when you have a lots of JOIN.

This is the code I now have.


SELECT hv.c.[RMS].value('./@V','float') RMS
  FROM [TS.Data].[dbo].[Devices] as d
  JOIN
  [TS.Data].[dbo].[Devices.Tests] as dt
  ON 
  d.[DeviceId] = dt.[DeviceId]
  JOIN
  [TS.Data].[dbo].[Devices.Tests.RMS] hv(c)
  ON dt.[DeviceTestId] = hv.[DeviceTestId]

Open in new window

0
Comment
Question by:ulf-jzl
  • 8
  • 3
  • 2
  • +1
14 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 36528571
did you try obtaining it as a recordset and assign as databale. Since there is no more detailed information I am not sure whether this helps.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 36528574
I thought you are using programming language.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 36528579
if you want to do without programming then this links should help

http://weblogs.sqlteam.com/mladenp/archive/2007/06/18/60235.aspx
http://support.microsoft.com/kb/316005
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:ulf-jzl
ID: 36528589
I just want to run this in MSSMS and it should return the data as a table. but it wont work.

dwkor: got his for me. and it does what I want.. but I can't get this to work on my tables with my joins


declare        @X xml = N'<RMS><R V="1"/>><R V="3"/>><R V="2"/></RMS>'        select        t.c.value('./@V','int')from        @X.nodes('/RMS/R') as t(c)

Open in new window

0
 
LVL 9

Expert Comment

by:s_chilkury
ID: 36528594
You have similar <v> tags due to which its hard to understand by the following query:

Check the following link:

http://stackoverflow.com/questions/192398/select-xml-nodes-as-rows
0
 

Author Comment

by:ulf-jzl
ID: 36528597
This is easier to read-
declare @X xml = N'<RMS><R V="1"/>><R V="3"/>><R V="2"/></RMS>'        
select
  t.c.value('./@V','int')
from        
  @X.nodes('/RMS/R') as t(c)

Open in new window

0
 

Author Comment

by:ulf-jzl
ID: 36528610
I like to the same with this
<RMS><V>1.45</V><V>4.35</V><V>3.65</V><</RMS>

but my problem is that I need to use JOIN to each it.
0
 

Author Comment

by:ulf-jzl
ID: 36528621
The RMS values are in the third table.

[Devices] -> [Devices.Tests] -> [Devices.Tests.RMS]
0
 

Author Comment

by:ulf-jzl
ID: 36528632
Or must I always get the data first into a variable of type xml and then use dwkor's query?

So a stored proc is needed to do this?

Or is it  away to fix it with only one query?
0
 
LVL 4

Accepted Solution

by:
nost2 earned 500 total points
ID: 36528782
You can do the select like this:
SELECT t.c.query('.').value('.','float') 
FROM [Devices] as d
  JOIN
  [Devices.Tests] as dt
  ON 
  d.[DeviceId] = dt.[DeviceId]
  JOIN
[Devices.Tests.RMS] hv
on dt.DeviceTestId = hv.DeviceTestId
CROSS APPLY RMS.nodes('/RMS/V') as t(c)

Open in new window

0
 

Author Comment

by:ulf-jzl
ID: 36528848
nost2: that works perfekt!!
0
 

Author Comment

by:ulf-jzl
ID: 36528868
nost2: can you briefly explain how this query works, new to query xml.

The
CROSS APPLY Havg.nodes('/Havg/V') as t(c)
and  
t.c.query('.').value('.','float')
0
 
LVL 4

Expert Comment

by:nost2
ID: 36529016
CROSS APPLY lets you use a table-valued function in a query. Nodes is a method for returning nodes of an XML document. Query and value are for filtering nodes and extracting values from the nodes, in this case we are interested in all of the contents of all the nodes so we use '.'.
0
 

Author Closing Comment

by:ulf-jzl
ID: 36529029
Thx man.. One word, Perfect!!! :)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
SYbase 4 30
Query Help - MSSQL - Averages 5 27
TSQL query to generate xml 4 33
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 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