Solved

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

Posted on 2010-11-11
10
360 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
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Rewrite without the NULLIF 4 25
SQL Server Express or Standard? 5 18
SQL Syntax 6 36
UPDATE JOIN multiple tables 5 13
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

679 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