• I'm not sure if this gives the correct results. I can't compare it to anything but it should give you something to start with.

    ;WITH Sample AS(

    SELECTI.StockCode,

    B.ParentPart,

    B.Component,

    B.QtyPer,

    B.Route,

    0 AS [level]

    from #BomSteve B

    JOIN #InvSteve I ON I.StockCode = B.ParentPart

    UNION ALL

    SELECTs.StockCode,

    B.ParentPart,

    B.Component,

    B.QtyPer,

    B.Route,

    s.[level] + 1 AS [level]

    from Sample s

    JOIN #BomSteve B ON s.Component = B.ParentPart

    )

    SELECT *

    FROM Sample

    ORDER BY StockCode, [level], ParentPart, Component

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2