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

  • SEARCH
  • Home
  • 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 391953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:06:41+00:00 2026-05-12T16:06:41+00:00

I have a parent/child relation table, many to many parentid int not null childid

  • 0

I have a parent/child relation table, many to many

parentid int not null
childid int not null

let say I have company a, b, c, d, e

a
..b
....c
..c
d
..e
....b
......c

I got this query to return one top parent

FUNCTION [dbo].[getRootCompagny] 
(
    @root int
)
RETURNS int
AS
BEGIN
DECLARE @parent int

SELECT @parent = companyparentid from companyrelation where companychildid = @root
if @parent is not null
   set @root = dbo.getRootCompagny(@parent)

RETURN @root
END

If I pass b, I will only get

a
..b
....c
..c

because the query I wrote can only manage one top parent.
How would you fix it to be able to get the whole tree, like the first one?

here is my CTE

    PROCEDURE [dbo].[GetCompanyRelation]
    @root int
AS
BEGIN
    SET NOCOUNT ON;

            set @root = dbo.getRootCompagny(@root)

    WITH cieCTE(CompanyChildid, CompanyParentid, depth, sortcol)
        AS
        (
            -- root member
            SELECT @root
                    , null 
                    , 0
                    , CAST(@root AS VARBINARY(900))


            UNION ALL

            -- recursive member
            SELECT R.CompanyChildid
                    , R.CompanyParentid
                    , C.depth+1
                    , CAST(sortcol + CAST(R.CompanyChildid AS BINARY(4)) AS VARBINARY(900))
            FROM CompanyRelation AS R JOIN cieCTE AS C ON R.CompanyParentid = C.CompanyChildid
        )

        -- outer query
        SELECT cieCTE.depth
                , cieCTE.CompanyChildid as ChildID
                , cieCTE.CompanyParentid as ParentId
                , company.[name] as [Name]
        FROM cieCTE inner join company on cieCTE.CompanyChildid = company.companyid
        ORDER BY sortcol
END

in the end, with the logic above, I get a, how to get a,d?

  • 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-05-12T16:06:42+00:00Added an answer on May 12, 2026 at 4:06 pm

    OK then, will you have a look at this. It gives the indents and a full tree path

        DECLARE @Table TABLE(
            ID VARCHAR(10),
            ParentID VARCHAR(10)
    )
    
    --INSERT INTO @Table (ID,ParentID) SELECT 'a', NULL
    INSERT INTO @Table (ID,ParentID) SELECT 'b', 'a'
    INSERT INTO @Table (ID,ParentID) SELECT 'c', 'a'
    INSERT INTO @Table (ID,ParentID) SELECT 'c', 'b'
    
    --INSERT INTO @Table (ID,ParentID) SELECT 'd', NULL
    INSERT INTO @Table (ID,ParentID) SELECT 'e', 'd'
    INSERT INTO @Table (ID,ParentID) SELECT 'b', 'e'
    
    DECLARE @Start VARCHAR(10)
    SELECT @Start = 'e'
    
    ;WITH   roots AS (
            SELECT  *
            FROM    @Table
            WHERE   ID = @Start
            UNION   ALL
            SELECT  DISTINCT
                    NULL,
                    ParentID
            FROM    @Table
            WHERE   ParentID = @Start
            AND     ParentID NOT IN ( SELECT ID FROM @Table)
            UNION ALL
            SELECT  t.*
            FROM    @Table t INNER JOIN
                    roots r ON t.ID = r.ParentID
    )
    ,       layers AS(
            SELECT  ParentID AS ID,
                    CAST(NULL AS VARCHAR(10)) AS ParentID,
                    CAST('' AS VARCHAR(MAX)) AS DisplayDepth,
                    CAST(ParentID + '\' AS VARCHAR(MAX)) AS LayerPath
            FROM    roots
            WHERE   ParentID NOT IN ( SELECT ID FROM @Table)
            UNION   ALL
            SELECT  t.*,
                    DisplayDepth + '[-]',
                    LayerPath + t.ID + '\'
            FROM    @Table t INNER JOIN
                    layers l ON t.ParentID = l.ID
    )
    SELECT  *
    FROM    layers
    ORDER BY LayerPath
    

    Is that what you had in mind?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables (say parent and child table and 1 to many relation),
I have a Table where Parent Child Relation ship is stored as: ChildID Name
Lets say I have two tables - child and parent with many-to-one relation. What
I have a one-to-many relation between Parent and Child table. In the parent object
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have a table like create table adjacencies( relationId int not null primary key
I have a table like create table adjacencies( relationId int not null primary key
I have a table with parent child relation I want help on recursive query
We have a table with a parent-child relation and would like to get it
I have table in that there are relation between Parent and Child, in this

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.