If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Selecting into XML format

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-12, 07:22
KvikLee KvikLee is offline
Registered User
 
Join Date: Jan 2012
Posts: 1
Selecting into XML format

Hi there

I have a few tables in MSSQL Server 2008 from which I need to select data into xml format.

The structure for the result XML is as follows:

PersonAccount
.........Address
.........EmployeeProfile
.............Address
.........GuestProfile
.............Address
.........StudentProfile
.............Address

So each person account has an adress together with 3 different profiles, all with addresses coming from the same table address.

Tables PersonAccount, EmployeeProfile, GuestProfile and StudentProfile are linked via PersonID and all 4 tables contain an AddressID linking to table Address.

Can anyone help me to do a select on these tables to get the xml structure above?

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-11-12, 10:23
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
FOR XML

Do you have SQL Server Books Online (BOL)???

What's New in FOR XML in Microsoft SQL Server 2005
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #3 (permalink)  
Old 01-11-12, 13:36
homerow homerow is offline
Registered User
 
Join Date: Sep 2011
Location: Greenville, SC USA
Posts: 28
RE: Selecting into XML format

Here's a quick SELECT framework you can use...

Code:
select pa.*
 ,(select * from Address where AddressID = pa.AddressID for xml path('Address') ,type) -- PA Address

 ,(select pf.* ,(select * from Address where AddressID = pf.AddressID for xml path('Address') ,type) -- EP Address
   from EmployeeProfile pf (nolock)
   where pf.PersonID = pa.PersonID
   for xml path('EmployeeProfile') ,type
  ) -- EmployeeProfile

 ,(select pf.* ,(select * from Address where AddressID = pf.AddressID for xml path('Address') ,type) -- GP Address
   from GuestProfile pf (nolock)
   where pf.PersonID = pa.PersonID
   for xml path('GuestProfile') ,type
  ) -- GuestProfile

 ,(select pf.* ,(select * from Address where AddressID = pf.AddressID for xml path('Address') ,type) -- SP Address
   from StudentProfile pf (nolock)
   where pf.PersonID = pa.PersonID
   for xml path('StudentProfile') ,type
  ) -- StudentProfile
from PersonAccount pa (nolock)
for xml path('PersonAccount') ,root('PersonAccounts') ,type
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On