Solved

selecting XML from sql2005

Posted on 2010-11-11
4
224 Views
Last Modified: 2012-05-10

 
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' )

Open in new window


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)
0
Comment
Question by:realcoding
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34114719
Something like this?
select Name heading, ReleaseID id, EndDate enddate,

    (select projectname heading, projectid id, 

        (select projectname heading, projectid id 

         from project subproject 

         where subproject.parentprojectid = project.projectid 

         order by projectid for xml auto, type) 

     from project project

     where parentprojectid is null

        and project.ReleaseID = Release.ReleaseID

     order by projectid 

     for xml auto, type)

from (select Release.Name, Release.ReleaseID, ReleaseSchedule.EndDate

    from Release Release

    inner join ReleaseSchedule ReleaseSchedule on Release.ReleaseID = ReleaseSchedule.ReleaseID) Release

order by Release.ReleaseID

for xml auto

Open in new window

0
 

Author Comment

by:realcoding
ID: 34115086
yes, close but the code has the same problem the code i just made... there are dups all over !!!

i get many dups on release, project and sub-project nodes.

here is the code i have that produces similar results to the sample above (both have dup data all over in the xml)

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
		 ORDER BY p2.ProjectName
         FOR XML PATH('SubProject'), TYPE
     )
     FROM dbo.Project p
     WHERE p.ReleaseId = r.ReleaseId and p.ParentProjectId is null
	 ORDER BY p.ProjectName
     FOR XML PATH('Project'), TYPE
    ) 
FROM dbo.Release r
INNER JOIN dbo.ReleaseSchedule rs ON r.ReleaseId = rs.ReleaseID
order by r.ReportingPriority
FOR XML PATH('Release'), ROOT('data')

Open in new window

0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 34115462
I'm not seeing any duplicates. Are you running this against different test data?

<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>
</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" />
0
 

Author Closing Comment

by:realcoding
ID: 34115677
great, sorry i needed a where clause and fixed everything cause my actual data is more complex than the sample.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

16 Experts available now in Live!

Get 1:1 Help Now