Solved

CTE, SQL Server, FOR XML

Posted on 2014-02-20
6
834 Views
Last Modified: 2014-02-21
I am trying to prepare some data for use with the command FOR XML EXPLICIT, I started by investigating this at Link and it seems simple enough.  I started to implement a nested CTE.  The code is below.

WITH Divi AS
(
      SELECT 0 AS 'Parent', di.DivisionID, di.DivisionNo,NULL AS 'DeptId', di.DivisionName, Null AS 'DepartmentNumber', CAST(NULL AS Varchar(50)) AS 'DepartmentName' from Division di
      UNION ALL
      SELECT dp.DivisionID AS 'Parent',NULL 'DivisionID' , NULL AS 'DivisionNo', dp.DeptId, Null AS 'DivisionName', dp.DepartmentNumber, dp.DepartmentName FROM Divi div  JOIN Department dp ON div.DivisionID = dp.DivisionID
)

SELECT * FROM Divi

It does the job of getting the data as one might expect.  The problem is I am unsure how to order it so that it would conform to the ordering seen in the link.
0
Comment
Question by:Alyanto
[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
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:Zberteoc
ID: 39874144
You will have to add an element of hierarchy and use it to sort. This example might help:

http://blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/
0
 
LVL 1

Author Comment

by:Alyanto
ID: 39876238
Unfortunately this does not answer my question but it does progress another area of the problem.  I would ultimately like the nesting of data to be output using the FOR XML EXPLICIT option.  This said the parent child relationships here need to be in order to ensure that the XML is in the correct order.
Sample XML
   <division name="One" id=1>
       <Department name="A" Id=1/>
       <Department name="B" Id=2/>
   </division>
<division name="Two" id=2>
       <Department name="C" Id=3/>
       <Department name="D" Id=4/>
   </division>

Sample table as expected
Parent    DivisionId   DepartmentId   DivisionName   DepartmentName    
NULL      1                 NULL                 One                   NULL
1             NULL          1                        NULL                 A
1             NULL          2                        NULL                 B
NULL      2                 NULL                 Two                   NULL
2             NULL          3                        NULL                 C
2             NULL          4                        NULL                 D

The noteable thing is the output order and the nesting is related.  Parent is an alias of Division Id found in the department table.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 39876449
So you order by Parent, DepartmentId.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:Alyanto
ID: 39876555
Ordering was one of the first things I tried unfortunately the result is that all of the NULL values appear at the top as a group.  The rest do sort themselves as you might expect and want.
0
 
LVL 27

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 39876562
Then sort by:

...
ORDER BY
       ISNULL(Parent,DivisionId),
       DepartmentId

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39877166
Cheers and thanks for you help, a perfect solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tempdb Contention - SQL SERVER 10 37
encrypt SQL Server 2008 port 1433 3 41
T-SQL: Only Wanting One Record 8 57
sql server string_split 4 21
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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