• If you're trying to unchain a sequence of part numbers using a parentID like this, you may want to look at using a recursive CTE to resolve the hierarchy.

    Something like this:

    CREATE TABLE #test (

    ID INT,

    ParentID INT,

    ColX CHAR(7)

    );

    GO

    INSERT INTO #test(ID,ParentID,ColX) VALUES (1, null, '100');

    INSERT INTO #test(ID,ParentID,ColX) VALUES (2,1, '100EXP1');

    INSERT INTO #test(ID,ParentID,ColX) VALUES (3,2, '100EXP2');

    INSERT INTO #test(ID,ParentID,ColX) VALUES (4,3, '100EXP3');

    WITH rCTE AS

    (

    SELECT ID, ParentID, ColX

    FROM #Test

    WHERE ColX = '100'

    UNION ALL

    SELECT b.ID, b.ParentID, b.ColX

    FROM rCTE a

    JOIN #Test b ON a.ID = b.ParentID

    )

    SELECT *

    FROM rCTE

    GO

    DROP TABLE #test;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St