Solved

Get data as a datatable from XML column

Posted on 2011-09-13
14
414 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

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.

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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

13 Experts available now in Live!

Get 1:1 Help Now