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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:45:44+00:00 2026-05-28T17:45:44+00:00

I have a basic recursive table setup in SQL 2008 with the following structure:

  • 0

I have a basic recursive table setup in SQL 2008 with the following structure:

CREATE TABLE [dbo].[Employee]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[ManagerId] [int] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Id] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[Employee]  WITH CHECK ADD  CONSTRAINT [FK_Employee_Employee] FOREIGN KEY([ManagerId]) REFERENCES [dbo].[Employee] ([Id])

Based on a couple of different articles online, I found where I can write a query such as the following to return a “manager” and all child records contained underneath:

WITH DirectRelationships AS
(
SELECT Id, Name, ManagerId, Depth = 0
FROM Employee
WHERE ManagerId = 1
UNION ALL
SELECT e.Id, e.Name, e.ManagerId, Depth = Depth + 1
FROM Employee e
INNER JOIN DirectRelationships DR ON DR.Id = e.ManagerId
WHERE Depth <= 2 /* this doesn't work */
)
SELECT * FROM DirectRelationships

When I run the select statement above without the “WHERE Depth <= 2” I get four columns back (Id, Name, ManagerId, Depth) that show the exact records I am looking for. The problem I run into is Depth is based on the overall depth from the root record (where ManagerId is null) and not the depth the record is from the requested manager.

For instance, assume I have the following data in this table:

select * from Employee;

Id  | Name             | ManagerId | Depth
------------------------------------------
1    CEO                 0           0
2    National Director   1           1
3    Regional Director   2           2
4    Store Director      3           3
5    Clerk               4           4
6    Clerk #2            4           4

What I want my query to be able to do is get a manager level (say, National Director or Id #2) and specify the number of levels to recursively pull (say, 2). That would return the following:

Id  | Name             | ManagerId | Depth
------------------------------------------
2    National Director   1           0 /*top level requested */
3    Regional Director   2           1 /*new depth based on requested parent */
4    Store Director      3           2 /*new depth based on requested parent */

So I wouldn’t get the top-level CEO position and I also wouldn’t get the individual clerks because I only wanted a specific entry and 2 levels below that. I’ve seen where you can specify MAXRECURSION in a query, but that throws an error if the level exceeds the number you specify which isn’t desirable. The hierarchical structure I’ll be dealing with could have thousands of records and for any specific entity listing I’ll only want up to 5 of the children levels with the others being irrelevant.

Any help would be much appreciated!

  • 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-28T17:45:46+00:00Added an answer on May 28, 2026 at 5:45 pm

    Try running:

    WITH DirectRelationships AS
    (
        SELECT Id, Name, ManagerId, Depth = 0
        FROM Employee
        WHERE Id = 2
    
        UNION ALL
    
        SELECT e.Id, e.Name, e.ManagerId, Depth = Depth + 1
        FROM Employee e
            INNER JOIN DirectRelationships DR ON DR.Id = e.ManagerId
    )
    SELECT * 
    FROM DirectRelationships 
    WHERE Depth <= 2
    

    You should get the result you are looking for:

    Id Name              ManagerId Depth
    -- ----------------- --------- -----
    2  National Director 1         0
    3  Regional Director 2         1
    4  Store Director    3         2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic understanding of mock and fake objects, but I'm not sure
I have not worked with XML much so this may be a basic question
I have basic knowledge about SQL. But I still have difficulty when write SQL
Let's say I have a basic recursive function: function recur(data) { data = data+1;
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear
Currently, i have basic C++ and PHP skills. But, i want to switch to
I have a basic form with controls that are databound to an object implementing
I have a basic CRUD form that uses PageMethods to update the user details,
I have a basic model in which i have specified some of the fields
I have a basic ActiveRecord model in which i have two fields that i

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.