Solved

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

Posted on 2010-11-11
10
361 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Incorrect syntax near the keyword 'INNER' 3 37
What type of testing am I doing? 4 75
sql trace 4 27
T-SQL: Number of Records is Greater Than One 7 50
I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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