Solved

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

Posted on 2010-11-11
10
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
can you send me a sample of the database you are working on?
0
 

Author Comment

by:realcoding
Comment Utility
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
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:realcoding
Comment Utility
doesnt work @amgad
0
 
LVL 75

Expert Comment

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

Expert Comment

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

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

771 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

10 Experts available now in Live!

Get 1:1 Help Now