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 7001245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:46:43+00:00 2026-05-27T20:46:43+00:00

Is it possible to pass a parameter into a CTE that selects a node

  • 0

Is it possible to pass a parameter into a CTE that selects a node then selects its parent up to the root where the parentId is null?

In my code below if I pass in a parameter that selects Rain Coats and then recurses up the tree to mens wear where its parentId is null and selects all the nodes in that branch including children. Could someone help me with this please. My example just recurses and shows the depth

SQL example:

DECLARE @Department TABLE
(
    Id INT NOT NULL,
    Name varchar(50) NOT NULL,
    ParentId int NULL
)

INSERT INTO @Department SELECT 1, 'Toys', null
INSERT INTO @Department SELECT 2, 'Computers', null
INSERT INTO @Department SELECT 3, 'Consoles', 2
INSERT INTO @Department SELECT 4, 'PlayStation 3', 3
INSERT INTO @Department SELECT 5, 'Xbox 360', 2
INSERT INTO @Department SELECT 6, 'Games', 1
INSERT INTO @Department SELECT 7, 'Puzzles', 6
INSERT INTO @Department SELECT 8, 'Mens Wear', null
INSERT INTO @Department SELECT 9, 'Mens Clothing', 8
INSERT INTO @Department SELECT 10, 'Jackets', 9
INSERT INTO @Department SELECT 11, 'Rain Coats', 10

;WITH c 
AS
(
    SELECT Id, Name,1 AS Depth
    FROM @Department
    WHERE ParentId is null  

         UNION ALL

         SELECT t.Id, t.Name, c.Depth + 1 AS 'Level'
    FROM @Department T  
    JOIN c ON t.ParentId = c.Id

)
SELECT * FROM c WHERE c.Id = 3
  • 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-27T20:46:44+00:00Added an answer on May 27, 2026 at 8:46 pm

    Your current CTE just shows all the items in the tree, with their Depth and all the other properties. As such, it works fine.

    To do what you’re looking for, you have to almost “invert” the CTE – grab the item you’re interested in first, as the “anchor” of your CTE, and then “recurse up” to the root:

    DECLARE @StartID INT = 11
    
    ;WITH c 
    AS
    (
        SELECT Id, ParentId, Name, 1 AS Depth
        FROM @Department
        WHERE Id = @startID
    
        UNION ALL
    
        SELECT t.Id, t.ParentId, t.Name, c.Depth + 1 AS 'Level'
        FROM @Department T  
        INNER JOIN c ON t.Id = c.ParentId
    )
    SELECT * 
    FROM c 
    

    This will do what you’re looking for and output:

    Id ParentId  Name            Depth
    11    10     Rain Coats        1
    10     9     Jackets           2
     9     8     Mens Clothing     3
     8   NULL    Mens Wear         4
    

    Update

    For a reverse order of Depth you can use this:

    ;WITH c 
    AS
    (
        SELECT Id, ParentId, Name, 1 AS Depth
        FROM @Department
        WHERE Id = @startID
    
        UNION ALL
    
        SELECT t.Id, t.ParentId, t.Name, c.Depth + 1 AS 'Level'
        FROM @Department T  
        INNER JOIN c ON t.Id = c.ParentId
    )
    SELECT Id,
           ParentID, 
           Name,
           MAX(Depth) OVER() - Depth + 1 AS InverseDepth
    FROM c
    

    Output from this:

    Id ParentId  Name            InverseDepth
    11    10     Rain Coats        4
    10     9     Jackets           3
     9     8     Mens Clothing     2
     8   NULL    Mens Wear         1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to pass a jQuery method as a parameter into a function
i was wondering if it is possible to pass a parameter into a select
I would like to know if it's possible to pass a parameter to a
Is it possible to pass a list constructor parameter when resolving a type? I
In Ruby, is it possible to pass by reference a parameter with value-type semantics
In VB.NET, it is entirely possible to pass an integer as a string parameter
Possible Duplicate: How do you pass a function as a parameter in C? Is
I'd like to pass a table as a parameter into a scaler UDF. I'd
Is it possible to pass 3 parameters into a selenium command via IDE? e.g.
I'm trying to pass a parameter from php into my javascript function inside html.

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.