Solved

Using for xml to structre table data as xml

Posted on 2011-02-17
4
346 Views
Last Modified: 2012-05-11
Assume I have the following table in SQL.

module      table_name      column_display
clients      Client Info         ID number
clients      Client Info       First name
clients      Client Info       Last name
clients      Client Info       Mid init
clients      Client Info       SSN
clients      Client Info       Birthdate
clients      Client Info       Sex
clients      Client Info       Race
clients      Client Info        State ID / DL Number
clients      Client Info       ID / DL State
clients      Client Info        Marital Status
clients      Client Info       Follow Up

How can I construct a for xml query that will return the data as xml in the following format:

<clients>
 <table="client info">
      <field name="ID number" />
      <field name="First name" />
      <field name="Last name" />
      <field name="Mid init" />
      <field name="SSN" />
      <field name="Birthdate" />
      <field name="Sex" />
      <field name="Race" />
      <field name="State ID / DL Number" />
      <field name="ID / DL State" />
      <field name="Marital Status" />
      <field name="Follow Up" />
 </table>
</clients>
0
Comment
Question by:hyphenpipe
  • 2
4 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 34920652
IF you know .NET then you could use the SqlXmlCommand Object


See --> http://msdn.microsoft.com/en-us/library/aa257414(v=sql.80).aspx
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 34920881
I am looking for a way to do this from a SQL query such as select....for xml path('')

Thanks.
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 34921417
Hi,

Try this

/peter
select 1 as tag,
		null as parent,
		null [clients!1],
		null [table!2!name],
		null [field!3!name]
from atable 
union 
select 2 as tag,
	   1 as parent,
	   null,
	   table_name,
	   null
from atable 
union 
select 3 as tag,
	   2 as parent,
	   null,
	   null,
	   column_display
from atable 
for xml explicit

Open in new window

0
 
LVL 6

Author Closing Comment

by:hyphenpipe
ID: 34925419
Perfect solution.

Thanks!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

930 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

12 Experts available now in Live!

Get 1:1 Help Now