Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:09:22+00:00 2026-06-12T06:09:22+00:00

This might seem a bit silly, but I want to try and solve this

  • 0

This might seem a bit silly, but I want to try and solve this query in one go.

I have a table in a SQL database that links back onto itself. It has an Organization ID and a Parent Organization ID. Top level orgs have a parent ID of 0, and each org at a different level has a parent org of one of the orgs above it. Level of the org used to be determined by another field in the table called org Level. However, now the level is determined by how far down the tree the org is. Such that if an org had a parent that had another parent, that first org would be at level 3, it’s parent at level 2, and it’s grandparent at level 1.

I understand how I want my SQL to work, but the problem is creating a generic SQL statement that will return me the orgs and their levels. I decided to try and use XML to do this, but this might not be the best idea. Let’s say I have an org structure like this.

-Top Org 1
--Second Org 1
--Second Org 2
-Top Org 2
--Second Org 3
--Second Org 4
-Top Org 3
--Second Org 5
--Second Org 6

When done, I want to query an XML that looks like this:

<Organizations>
  <Lvl1 OrgID="1" Name="Top Level 1" Index="1">
    <Lvl2 OrgID="5" Name="Second Level 1" Index="1" />
    <Lvl2 OrgID="6" Name="Second Level 2" Index="2" />
  </Lvl1>
  <Lvl1 OrgID="3" Name="Top Level 2" Index="2">
    <Lvl2 OrgID="7" Name="Second Level 3" Index="1" />
    <Lvl2 OrgID="8" Name="Second Level 4" Index="2" />
  </Lvl1>
  <Lvl1 OrgID="4" Name="Top Level 3" Index="3">
    <Lvl2 OrgID="9" Name="Second Level 5" Index="1" />
    <Lvl2 OrgID="10" Name="Second Level 6" Index="2" />
  </Lvl1>
</Organizations>

I have figured out how I want to do this normally, if I know how many levels I want to query for. If I want to query the first and second levels, I do this:

SELECT
    Lvl1.Org_ID [@OrgID]
    ,Lvl1.Org_Name [@Name]
    ,Lvl1.Org_SortID [@Index]
    ,(
        SELECT
            Lvl2.Org_ID [@OrgID]
            ,Lvl2.Org_Name [@Name]
            ,Lvl2.Org_SortID [@Index]
        FROM Organizations Lvl2
        WHERE Lvl2.Org_ParentID = Lvl1.Org_ID
        GROUP BY Lvl2.Org_ID, Lvl2.Org_Name, Lvl2.Org_SortID
        ORDER BY Lvl2.Org_SortID
        FOR XML PATH('Lvl2'), TYPE
    )
FROM Organizations Lvl1
INNER JOIN Organizations Child1 ON Lvl1.Org_ID = Child1.Org_ParentID
WHERE Lvl1.Org_ParentID = 0
GROUP BY Lvl1.Org_ID, Lvl1.Org_Name, Lvl1.Org_SortID
ORDER BY Lvl1.Org_SortID
FOR XML PATH('Lvl1'), ROOT('Organizations')

But let’s pretend that I don’t know how many levels I’m going to have. I might have only two levels, I might have 15 levels. And not every org could have the same number of levels. What I want to find out is if there’s a way to recursively look at each record as it comes in and determine if it has any children under it. If so, create new XML tags for those children and drill down one level, then check each child and see if THEY have children. Ect.

Maybe there’s not a good way of doing this, or many it’s a huge memory and time sink to make SQL do something like this. But if it’s not, let me know that it’s possible and show me an example. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:09:24+00:00Added an answer on June 12, 2026 at 6:09 am

    I had a similar problem recently that I resolved by using a recursive function (i.e. it calls itself).

    I have done no testing but something like this should work:

    CREATE FUNCTION dbo.GetChildOrganisations (@ParentID INT)
    RETURNS XML
    AS
    BEGIN
        RETURN 
        (   SELECT  Org_ID [@OrgID],
                    Org_Name [@Name],
                    Org_SortID [@Index],
                    dbo.GetChildOrganisations(Org_ID) [ChildOrganisations] 
            FROM    Organizations 
            WHERE   Org_ParentID = @ParentID
            FOR XML PATH('Orgainisations')
        )
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might seem a bit awkward but I want to start a console program
I know this might seem a bit odd, but i have this requirement and
This might seem a bit weird, but since I am new in RoR and
This might seem like a trivial question, but I'm a bit muddled in my
This might seem as silly question but I am thinking there might be the
I have read a bit about lazy loading in c# and this might seem
This might seem a bit like a do-my-homework-for-me question (and it is), but I
Ok this may seem like a bit of a noob question but one many
This question might seem a bit bizarre, but... I run a mafia style game
This might seem a bit odd to ask, but I couldn't get an answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.