updating a table based on another table with concatenated results

  • [X]

    [ID] [int] NOT NULL,

    [Type] [nvarchar]

    [Plannned] [nvarchar]

    [Y]

    [ID] [int] NOT NULL,

    [Type] [nvarchar](5)

    [Description] [nvarchar]

    Contents in Y are as

    ID Type Description

    1 H For 1

    1 H For 2

    1 H For 3

    1 HFor 4

    Now, I want the contens in y to be updated in X as

    ID Type Planned

    1 H For 1, For 2, For 3, For 4

    Deos anyone know how i could accomplish this

    I can create a new column in Y with combined results of same ID and then update X but i donot know if there is another way of doing this

    Thanks

  • Try this

    insert into @Table1(id,type,planned)

    SELECT p1.id,p1.type,

    ( SELECT description + ','

    FROM @Table2 p2

    WHERE p2.id = p1.id and p2.type=p1.type

    ORDER BY description

    FOR XML PATH('') ) AS planned

    FROM @Table2 p1

    GROUP BY id,type ;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply