Welcome to Sol 3 Sign in | Join | Help
CS Search | Live Search Search

Sol 3

Home of Barrows Software Solutions, LLC

Keith Barrows

Subjects range from Personal to Technical.

SQL XML - TreeView

I sometimes forget how to do an XML output formed in a tree when dealing with a single table parent/child relationship.  There is a great explanation on SQL Server Central on *how* to do this.  I am mainly capturing the link and the SQL I just generated to do this.  This query will drill down 12 levels at the most.  Just alter the case statement (pivot) to go deeper.

ALTER PROC getOrgUnitTreeAsXml
AS
BEGIN
    ;WITH OrgUnit1
    AS
    (
        SELECT
            0 AS [Level],
            [OrgUnitId],
            [orgUnitParentID],
            [orgUnit],
            CAST( [orgUnitID] AS VARBINARY(MAX)) AS Sort
        FROM [orgUnit]
        WHERE [orgUnitParentID] IS NULL
        UNION ALL
        SELECT
            [Level] + 1,
            p.[OrgUnitId],
            p.[orgUnitParentID],
            p.[orgUnit],
            CAST( SORT + CAST(p.[orgUnitID] AS BINARY(4)) AS VARBINARY(MAX))
        FROM [orgUnit] p
        INNER JOIN OrgUnit1 c ON p.[orgUnitParentID] = c.[OrgUnitId]
    )
    ,  OrgUnit2 AS
    (
        SELECT
            [Level] + 1 AS Tag,
            [OrgUnitId],
            [orgUnitParentID],
            [orgUnit],
            sort
        FROM OrgUnit1
    )
    , OrgUnit3 AS
    (
        SELECT
            *,
            (SELECT Tag FROM OrgUnit2 r2 WHERE r2.[OrgUnitId] = r1.[orgUnitParentID]) AS ParentTag
        FROM OrgUnit2 r1
    )
    SELECT Tag, ParentTag as Parent,
    CASE WHEN tag = 1 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!1!id',
        CASE WHEN tag = 1 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!1!name',
    CASE WHEN tag = 2 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!2!id',
        CASE WHEN tag = 2 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!2!name',
    CASE WHEN tag = 3 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!3!id',
        CASE WHEN tag = 3 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!3!name',
    CASE WHEN tag = 4 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!4!id',
        CASE WHEN tag = 4 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!4!name',
    CASE WHEN tag = 5 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!5!id',
        CASE WHEN tag = 5 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!5!name',
    CASE WHEN tag = 6 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!6!id',
        CASE WHEN tag = 6 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!6!name',
    CASE WHEN tag = 7 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!7!id',
        CASE WHEN tag = 7 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!7!name',
    CASE WHEN tag = 8 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!8!id',
        CASE WHEN tag = 8 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!8!name',
    CASE WHEN tag = 9 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!9!id',
        CASE WHEN tag = 9 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!9!name',
    CASE WHEN tag = 10 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!10!id',
        CASE WHEN tag = 10 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!10!name',
    CASE WHEN tag = 11 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!11!id',
        CASE WHEN tag = 11 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!11!name',
    CASE WHEN tag = 12 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!12!id',
        CASE WHEN tag = 12 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!12!name'
    FROM OrgUnit3
    ORDER BY sort
    FOR XML EXPLICIT
END
GO

Published Monday, August 25, 2008 4:52 PM by Keith Barrows
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

ASPInsiders said:

I sometimes forget how to do an XML output formed in a tree when dealing with a single table parent/child

August 25, 2008 5:12 PM
 

Jared said:

Sweet, thanks for the clean example! I had fooled around with recursion when 2005 had just come out but promptly forgot about it :-D.

April 28, 2009 6:43 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Keith Barrows

I've been in computing since 1975. I started on an old PDP-8J with 3k of memory and 2 teletypes. I learned BASIC and Octal based assembly. I later moved into CPM, TR-DOS, Apple and finally into PC-DOS, Dr DOS and MS-DOS. I've been a beta tester for over a decade, got into web applications as a means to handle B2B requirements and have specialized in data movement between applications and businesses since. I have been a MVP, ASP Elite and was selected by Microsoft as one of the original 15 board members for ASPInsiders.
CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development