• steve.spenceST (3/20/2014)


    🙂 Thank you for this answer. Helped me greatly.

    If we consider the original post, THIS is what Luis is talking about...

    --===== Create the test tables with the OP data.

    -- This is NOT a part of the solution.

    SELECT a.ID, a.Name

    INTO #TableA

    FROM (

    SELECT 1,'AAA' UNION ALL

    SELECT 2,'BBB' UNION ALL

    SELECT 3,'CCC'

    ) a (ID,[Name])

    ;

    SELECT b.RowID, b.FID, b.Value

    INTO #TableB

    FROM (

    SELECT 1,1,111 UNION ALL

    SELECT 2,1,222 UNION ALL

    SELECT 3,2,333 UNION ALL

    SELECT 4,3,444 UNION ALL

    SELECT 5,3,555 UNION ALL

    SELECT 6,3,666

    ) b (RowID, FID, [Value])

    ;

    --===== Solve the solution using the concatenation abilities of FOR XML PATH.

    SELECT a.Name

    ,[Value] = STUFF(

    (SELECT ',' + CAST(b.Value AS VARCHAR(10))

    FROM #TableB b

    WHERE b.FID = a.ID

    ORDER BY b.Fid

    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')

    ,1,1,'')

    FROM #TableA a

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)