query(bit urgent pls help)

  • Hi all,

    can any one pls send the query

    I have two tables

    A B

    101 101 abc

    102 101 efg

    I need query which displays output as 101 abc,efg

  • Try This:

    CREATE TABLE #A (ID int)

    CREATE TABLE #B (ID int, Code varchar(10))

    INSERT INTO #A SELECT 1 UNION SELECT 2

    INSERT INTO #B

    SELECT 1,'abc' UNION SELECT 1,'def' UNION

    SELECT 2,'pqr' UNION SELECT 2,'lmn'

    SELECT * FROM #A

    SELECT * FROM #B

    Declare @a varchar(100)

    SELECT @a = COALESCE(@a+',','')+Code FROM #B WHERE ID = 1

    SELECT @a

    SET @a = NULL

    SELECT @a = COALESCE(@a+',','')+Code FROM #B WHERE ID = 2

    SELECT @a

    DROP TABLE #A

    DROP TABLE #B

  • Its working fine hari

    I Let u know whether it satisfies all my requirement.

  • Do you always want to run the query for a single ID? Or do you intend to run a single query and get the result for all the IDs? If so, you can achieve it using FOR XML PATH as explained here: How to generate a Delimited String using FOR XML PATH

    .

Viewing 4 posts - 1 through 3 (of 3 total)

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