Solved

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

Posted on 2010-11-11
10
357 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
 

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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

15 Experts available now in Live!

Get 1:1 Help Now