Solved

how to get sql to output as xml (with particular xml format)

Posted on 2010-11-11
10
362 Views
Last Modified: 2012-05-10
-- get release data here
SELECT 
 'release' as type,
 r.ReleaseId as id,
 r.Name, 
 rs.EndDate 
FROM Release r 
LEFT OUTER JOIN ReleaseSchedule rs ON r.ReleaseId = rs.ReleaseId AND rs.MilestoneCID = 77
WHERE r.CompletionStatusCID NOT IN (34, 35) 
ORDER BY r.ReportingPriority, r.Name 

-- get project data here
SELECT 
 'project' as type,
 p.ProjectId as id,
 p.ProjectName as name, 
 p.ParentProjectId, 
 p.ReleaseId 
FROM Project p 
WHERE p.CompletionStatusCID NOT IN (34, 35) AND p.ProjectTypeCID <> 92
ORDER BY p.ProjectName

Open in new window


this is the code that i am using to get data for a project management application in order to build a tree view or projects (recursively listed under parent project, as well as release.) that is to say that i return and/or  build a tree view such that the top nodes are Release's and drilling down into releases you get projects and sub projects.

the project table is self referencing and has a parentProjectId field to maintain the project - subProject relationship.

how can i get the data from the above SQL out as XML with the below formatting???



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

Open in new window

0
Comment
Question by:realcoding
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
ID: 34112949
you can get this using "FOR XML " keyword, check this link: http://blah.winsmarts.com/2007-2-Bak2Basics__Learn_T-SQL_-_FOR_XML.aspx
0
 

Author Comment

by:realcoding
ID: 34113027
i have been trying to but the formatting of the final xml is the problem.

more details would be a solution, thanks for the link but it doesn't help me
0
 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
ID: 34113078
can you send me a sample of the database you are working on?
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:realcoding
ID: 34113405
here is a stream lined structure of tables and some test data. you can ignore the where clauses in my original SQL - they are irrelevant for the purpose of getting this to work

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 (2, '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

0
 
LVL 4

Accepted Solution

by:
Amgad_Consulting_Co earned 500 total points
ID: 34115550
can you check that :
SELECT
 'release' as typec,
 r.ReleaseId as idr,
 r.Name,
 rs.EndDate ,
  'project' as typep,
 p.ProjectId as idp,
 p.ProjectName as name,
 p.ParentProjectId
FROM   dbo.Release r inner join  Project p  on r.ReleaseId  = P.ReleaseId
LEFT OUTER JOIN ReleaseSchedule rs ON r.ReleaseId = rs.ReleaseId AND rs.MilestoneCID = 77
WHERE r.CompletionStatusCID NOT IN (34, 35)
ORDER BY r.ReportingPriority, r.Name
FOR XML AUTO
0
 

Author Comment

by:realcoding
ID: 34150459
doesnt work @amgad
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34151250
I am afraid your script "doesnt work" with your original query either.
0
 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
ID: 34155037
What was wrong?  I tested it here and it was working!!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34160769
I am sorry, I was not clear.  I was referring to the query and script the author posted, I was pointing out that it does not compile.  But I am not the only one to point that out, see here in a near duplicate question:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_26609094.html#a34115545
FYI the code in http:#a34115454 doesn't help, there is no context and fields that do not exist in the structures you provided.
0
 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
ID: 34162013
Sorry, I was not clear too, I was asking the author what is wrong with him, I agree with you that the author scripts was not enough, but I was trying to aid him to find the solution.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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