Solved

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

Posted on 2011-03-17
5
769 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server: SNAPSHOT replication to include a newly added table. 2 30
Linked Server Issue with SQL2012 3 28
Text file into sql server 5 32
sql 2008 how to table join 2 28
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, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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