Solved

Using for xml to structre table data as xml

Posted on 2011-02-17
4
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 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