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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:17:25+00:00 2026-05-16T17:17:25+00:00

This is in SQL Server 2008. I plan to create two tables to represent

  • 0

This is in SQL Server 2008.

I plan to create two tables to represent a Bill of material. The Item Master will have data as follows :-

[ID] [Item Code] [Item Description]

1—- A001 —- Assembly 1
2—- B001 —- Sub Assembly 1 (Child of Assembly 1)
3—- B002 —- Sub Assembly 2 (Child of Assembly 1)
4—- C001—- Component 1 (Child of Sub Assembly 1)
5—- C002—- Component 2 (Child of Sub Assembly 2)

The BOM Relation Table will have data as follows . [Parent Item ID] and [Child Item ID] are Foreign keys to Item master. :-

[ID] [Parent Item ID] [Child Item ID]

1—- 1—- 2
2—- 1—- 3
3—- 2—- 4
4—- 3—- 5

So the first table has just the items themselves and the other table has relationships in terms of which parent ID has which children.

What could be the SQL to fetch all the child items for the Assembly (A001) given the fact that it may have to iterate recursively depending on data added to above tables ?

So for above data, i should get the output as follows :-

1) A001

                1.1) B001       
                               1.1.1)C001      
                1.2) B002         
                               1.2.1) C002  

Thanks,
Chak.

  • 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-16T17:17:25+00:00Added an answer on May 16, 2026 at 5:17 pm

    Use a recursive CTE. For example:

    WITH BomTree (ID, Level, [Item Code], [Item Description], Depth)
    AS
    (
    -- Anchor member definition
        SELECT m.*, 0 AS Depth
        FROM dbo.Master AS m
        WHERE m.[ID] = ?
        UNION ALL
    -- Recursive member definition
        SELECT m.*, t.Depth + 1 AS Depth
        FROM dbo.Master AS m
        INNER JOIN dbo.BOM AS b
            ON m.[ID] = b.[Child Item ID]
        INNER JOIN BomTree AS t
            ON b.[Parent Item ID] = t.ID
    )
    -- Statement that executes the CTE
    SELECT * FROM BomTree; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is SQL Server 2008. I have these two tables and a join: DECLARE
I'm using SQL Server 2008. I have two tables like this: OrderItems OrderItemID InventoryItemID
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I'm working on a SQL Server 2008 database. This database has two tables. Book
I have this SQL Server 2008 UDT: CREATE TYPE [dbo].[IdentityType] AS TABLE( [Id] [int]
I have SQL Server 2008 with a table called ProductCategories designed like this: Id
I have a SQL Server 2008 DB with a table like this (Table1): ID
I am using SQL Server 2008. This are my tables. Table A Aid int
I'm using SQL Server 2008. I'm creating a DDL trigger like this: CREATE TRIGGER
I currently have this statement in my SQL View (SQL Server 2008 R2) which

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.