Solved

Using for xml to structre table data as xml

Posted on 2011-02-17
4
337 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

18 Experts available now in Live!

Get 1:1 Help Now