Solved

Return attribute nodes from multiple element nodes using SQL 2008 and Xquery

Posted on 2011-03-17
5
760 Views
Last Modified: 2012-05-11
I have a table in SQL 2008 which contains one column as an xml data type.

This table has thousands of rows and for that xml column the xml data in that field would have a stucture like follows:

<client_info first_name="John" last_name="Doe">
<court date="08/01/2010">
<cs date="09/01/2010" />
<cs date="10/01/2010" />
</court>
<meeting date="08/05/2010">
<teacher date="08/06/2010" />
<teacher date="10/18/2010" />
</meeting>
</client>

How can I return, preferably as a single XML datagram, a document that has the value of every date attribute and the local-name of it's parent node bearing in mind this is just a simple XML example and there can and will be more levels to this document and it will need to be processed for each row on the table ideally returning a single XML datagram.

Any SQL/XML experts who can share a solution I would greatly appreciate it.
0
Comment
Question by:hyphenpipe
  • 5
5 Comments
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161119
The best I have come up with so far is:

select      t.c.value('local-name(.)', 'varchar(75)')
            , date = t.c.value('@date', 'datetime')
 from table
 cross apply column.nodes('//*') t(c)
 where t.c.exist('//@*[local-name(.) = "date"]') = 1

But that returns every node name in for the XML document if at has at least one date.

I'd like to return on one shot just every date and its parent node name.
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161127
Also, I notice my XML is not well formed.

The starting element node should be <client... and not <client_info...

I think you get the rest.
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161141
As an update, here is what I have so far:

declare @doc xml
set @doc = '<client first_name="John" last_name="Doe">
<court date="08/01/2010">
<cs date="09/01/2010" />
<cs date="10/01/2010" />
</court>
<meeting date="08/05/2010">
<teacher date="08/06/2010" />
<teacher date="10/18/2010" />
</meeting>
</client>'

select @doc

select      name = node.c.value('local-name(.)', 'varchar(75)')
            , date = node.c.value('@date', 'datetime')
 from @doc.nodes('//*') node(c)
 where node.c.exist('//@*[local-name(.) = "date"]') = 1
 for xml auto

Doing so returns this:

<node name="client" />
<node name="court" date="2010-08-01T00:00:00" />
<node name="cs" date="2010-09-01T00:00:00" />
<node name="cs" date="2010-10-01T00:00:00" />
<node name="meeting" date="2010-08-05T00:00:00" />
<node name="teacher" date="2010-08-06T00:00:00" />
<node name="teacher" date="2010-10-18T00:00:00" />

But I would like to eliminate that first node (client) before returning the XML datagram.

Again, I will also need to process this for a large table with thousands of rows but just one XML column.
0
 
LVL 6

Accepted Solution

by:
hyphenpipe earned 0 total points
ID: 35161177
Got it.

select      name = node.c.value('local-name(.)', 'varchar(75)')
            , date = node.c.value('@date', 'datetime')
 from @doc.nodes('//*[@*[local-name(.) = "date"]]') node(c)
 for xml auto

Thanks.

0
 
LVL 6

Author Closing Comment

by:hyphenpipe
ID: 35161179
Figured out my own solution.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
AD 2008  to 2012/16 ? 3 34
View Sql in stored procedure 13 20
Sql to find top3 for each record 7 18
t-sql splitting string column 5 26
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

14 Experts available now in Live!

Get 1:1 Help Now