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 3337848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:18:09+00:00 2026-05-18T00:18:09+00:00

CREATE TABLE [dbo].[Project]( [ProjectId] [int] NOT NULL, [ProjectName] [nvarchar](255) , [ParentProjectId] [int] null, [ReleaseId]

  • 0
CREATE TABLE [dbo].[Project](
    [ProjectId] [int] NOT NULL,
    [ProjectName] [nvarchar](255) ,
    [ParentProjectId] [int] null,
    [ReleaseId] [int] 
)

insert into Project values (1, 'Project 1', null, 1)
insert into Project values (2, 'Project 2', null, 1)
insert into Project values (3, 'project 3', 1, 1)
insert into Project values (4, 'project 4', 2, 1)



CREATE TABLE [dbo].[Release](
    [ReleaseId] [int] ,
    [Name] [nvarchar](255) ,
    [ReportingPriority] [int] 
)

insert into Release values (1, 'march release', 1)
insert into Release values (2, 'may release', 2)
insert into Release values (3, 'june release', 3)


CREATE TABLE [dbo].[ReleaseSchedule](
    [ReleaseScheduleID] [int] ,
    [ReleaseID] [int], 
    [EndDate] [datetime]
)

insert into ReleaseSchedule values (1, 1, '3/1/2010' )
insert into ReleaseSchedule values (2, 2, '5/1/2010' )
insert into ReleaseSchedule values (3, 3, '6/1/2010' )

This is the SQL data I have. From this I need to get a hierarchical XML that resembles this:

<Release Heading="releaseName" id="releaseID" EndDate="date">
  <Project Heading="projName" id="projectID">
    <SubProject Heading="subprojName" id="projectID"/>
    <SubProject Heading="subprojName" id="projectID"/>
  </Project>
  <Project Heading="releaseName" id="projectID">
    <SubProject Heading="subprojName" id="projectID"/>
  </Project>
</Release>

Basically the logic is that each release has some projects to it, and projects can be nested with sub-projects (from the project table’s self referencing data)

(note endDate comes from a join between the two release tables.)

  • 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-05-18T00:18:09+00:00Added an answer on May 18, 2026 at 12:18 am

    Try this query here:

    SELECT 
        r.NAME AS '@Heading',
        r.ReleaseId AS '@id',
        rs.EndDate AS '@EndDate',
        (SELECT 
            p.ProjectName AS '@Heading',
            p.ProjectId AS '@id',
            (SELECT 
                p2.ProjectName AS '@Heading',
                p2.ProjectId AS '@id'
             FROM dbo.Project p2
             WHERE p2.ParentProjectId = p.projectId
             FOR XML PATH('SubProject'), TYPE
         )
         FROM dbo.Project p
         WHERE p.ReleaseId = r.ReleaseId
         FOR XML PATH('Project'), TYPE
        ) 
    FROM dbo.Release r
    INNER JOIN dbo.ReleaseSchedule rs ON r.ReleaseId = rs.ReleaseID
    FOR XML PATH('Release'), ROOT('Releases')
    

    It gives me this output (based on your data provided):

    <Releases>
      <Release Heading="march release" id="1" EndDate="2010-03-01T00:00:00">
        <Project Heading="Project 1" id="1">
          <SubProject Heading="project 3" id="3" />
        </Project>
        <Project Heading="Project 2" id="2">
          <SubProject Heading="project 4" id="4" />
        </Project>
        <Project Heading="project 3" id="3" />
        <Project Heading="project 4" id="4" />
      </Release>
      <Release Heading="may release" id="2" EndDate="2010-05-01T00:00:00" />
      <Release Heading="june release" id="3" EndDate="2010-06-01T00:00:00" />
    </Releases>
    

    The FOR XML PATH approach, introduced in SQL Server 2005, makes it quite easy to define the exact structure of your output XML with elements and attributes, and with the FOR XML PATH(..), TYPE expression for sub selects, you can easily get nested result sets.

    Update: for the “duped” projects – maybe you need to add another WHERE clause to your first subquery that selects projects – select only those projects that have no parent (only top-level projects):

    (SELECT 
        p.ProjectName AS '@Heading',
        p.ProjectId AS '@id',
        (SELECT 
            p2.ProjectName AS '@Heading',
            p2.ProjectId AS '@id'
         FROM dbo.Project p2
         WHERE p2.ParentProjectId = p.projectId
         FOR XML PATH('SubProject'), TYPE
     )
     FROM dbo.Project p
     WHERE p.ReleaseId = r.ReleaseId
     AND p.ParentProjectId IS NULL  -- add this line to select only top-level projects
     FOR XML PATH('Project'), TYPE
    

    Update 2: since I start the selection on the Release table, yes, obviously, projects that aren’t assigned to any release will be left out. However, releases that have no assigned projects ought to show up – can you verify??

    What doesn’t work right now is having a release that is not assigned to a release schedule – you can easily change that by changing the outer-most query to:

    .....    
    FROM dbo.Release r
    LEFT OUTER JOIN dbo.ReleaseSchedule rs ON r.ReleaseId = rs.ReleaseID
    

    Use LEFT OUTER JOIN to list all releases – even those not assigned to any schedule.

    Basically, this is all pretty standard SQL query stuff – doesn’t really have anything to do with the XML specific aspects of your question, right??

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

Sidebar

Related Questions

Given a table such as: CREATE TABLE dbo.MyTestData (testdata varchar(50) NOT NULL) ALTER TABLE
Here's my table: CREATE TABLE `alums_alumphoto` ( `id` int(11) NOT NULL auto_increment, `alum_id` int(11)
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
Below is my SQL syntax: CREATE TABLE [dbo].[Security_Module_Info]( [Client_Company_ID] [smallint] NOT NULL, [Module_ID] [tinyint]
My table is: CREATE TABLE A( id serial NOT NULL, date timestamp without time
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
The following T/SQL:- create table #temp(foo varchar(10)) insert into #temp select '' insert into
I've a table [File] that has the following schema CREATE TABLE [dbo].[File] ( [FileID]
create table person ( name varchar(15), attr1 varchar(15), attr2 varchar(1), attr3 char(1), attr4 int
If I do a Create Table If Not Exists , and a table with

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.