Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using for xml to structre table data as xml

Posted on 2011-02-17
4
Medium Priority
?
389 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 2000 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: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Detach & Attach 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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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