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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:29:16+00:00 2026-05-16T12:29:16+00:00

I have a table with the following schema : ID , CatID, ParentCatID, SiteID

  • 0

I have a table with the following schema :

ID , CatID, ParentCatID, SiteID

I want to get all the sites that belong to the categories that are the roots ( means their ParentCatID = 0) and all their descendants.

for example :

ID , CatID, ParentCatID, SiteID
--------------------------------
1  , 2    , 0          , 3
1  , 4    , 2          , 6
1  , 5    , 4          , 7

In this example CatID 2 is the parent of 4 and 4 is the parent of 5.

How can I get all the SiteIDs that belongs to the root category and all its descendants.

  • 1 1 Answer
  • 3 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-16T12:29:17+00:00Added an answer on May 16, 2026 at 12:29 pm

    Using a recursive Common Table Expression, supported on SQL Server 2005+:

    WITH hierarchy AS (
      SELECT yt.id, 
             yt.catid,
             yt.parentcatid,
             yt.siteid
        FROM YOUR_TABLE yt
       WHERE yt.parentcatid = 0
      UNION ALL
      SELECT yt.id, 
             yt.catid,
             yt.parentcatid,
             yt.siteid
        FROM YOUR_TABLE yt
        JOIN hierarchy h ON h.catid = yt.catid)
    SELECT t.*
      FROM hierarchy t
    OPTION (maxrecursion 1000)
    

    If you get:

    The statement terminated. The maximum recursion 100 has been exhausted before statement completion

    The default is 100 recursions. The maximum number of recursions can be set via the maxrecursion option, up to a maximum of 32767.

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

Sidebar

Related Questions

I have a table name Discount that has the following schema: PK DiscountID int
I have a DB table that uses the following schema: CREATE TABLE users (id
Suppose that I have a table with the following schema: tableId field1 field2 .....
I have table with following table schema : I have created a method that
I have a bunch of coordinates in my table grid with the following schema
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have no control over database schema and have the following (simplified) table structure:
I have a table with the following schema: +------------------+ |Field | Type | +------------------+
I have test_scores table with following fields: Table schema: id (number) score1 (number) score2
Assuming I have the following schema table A: ID int primary key value varchar(255)

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.