Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:57:55+00:00 2026-06-12T06:57:55+00:00

I am using SQL Server 2008 and I’m trying to produce a custom xml

  • 0

I am using SQL Server 2008 and I’m trying to produce a custom xml using EXPLICIT mode with FOR XML.

I have the one to many relation having following query

select  
    p.personid, p.firstname, a.P_City 
from 
    tblPeople p with (nolock) 
left outer join 
    tblAddresses a with (nolock) on p.personid = a.personid 
where 
    p.personid = 120773

enter image description here

I want person as parent and address as child may be multiple because people to address having one to many relation.

I wrote the following query

 select 1 as TAG,
        null as parent, 
        p.personid as [person!1!personid],
        p.FirstName as [person!1!firstname],

        null as [addr!2!] 
        from tblPeople p with (nolock) where p.PersonID in (120773,117396)

        union all 

select 2,1, 
        p.PersonID, p.FirstName, a.P_City from tblAddresses a with(nolock), tblPeople p 
        where p.PersonID=a.PersonID and p.PersonID in (120773,117396)

        for xml explicit

Output as follows, it is broken xml with person nested there is some thing wrong with my code for sure.

<person personid="117396" firstname="David"/>
    <person personid="120773" firstname="Doyle">
        <addr>Mount Rainier</addr>
        <addr>Annapolis</addr>
</person>

Can some one please help me out !!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:57:56+00:00Added an answer on June 12, 2026 at 6:57 am

    I would recommend to ditch FOR XML EXPLICIT and use FOR XML PATH/ROOT instead.

    Using this query here:

    select  
        p.personid AS "@ID", 
        p.firstname, p.LastName,
        (
            SELECT AddressID AS "@ID", City
            FROM dbo.Address a 
            WHERE a.PersonID = p.PersonID
            FOR XML PATH('addr'), TYPE
        ) AS Addresses
    from 
        Person p with (nolock) 
    where 
        p.personid = 120773
    FOR XML PATH('Person'), ROOT('People')
    

    you’ll get this output XML:

    <People>
      <Person ID="120773">
        <firstname>John</firstname>
        <LastName>Doyle</LastName>
        <Addresses>
          <addr ID="1">
            <City>Annapolis</City>
          </addr>
          <addr ID="2">
            <City>Mount Rainier</City>
          </addr>
        </Addresses>
      </Person>
    </People>
    

    Tweak it as necessary. Read more details about FOR XML PATH on MSDN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008. I have a table A which accepts many
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
I am using SQL Server 2008 R2, I have a script to update the
I'm using SQL Server 2008. I have a table with x amount of rows.
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
Hi i am using SQL Server 2008 R2, i have lots of procedures in
I am using SQL Server 2008 R2, and I have created a schema Test
Using SQL server 2008. I have a family tree of animals stored in a
Using SQL Server 2008. I have a table variable with a single column and
Using SQL Server 2008, I am trying to do something similar to this post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.