• Jeff Moden (6/23/2016)


    curious_sqldba (6/22/2016)


    Sean Lange (6/22/2016)


    curious_sqldba (6/22/2016)


    Thanks. This is just displaying, i want to store the data in the variable and then use the variable different places.

    Do you know how to use a select statement to populate a variable? It is no different here.

    However, I have a feeling that you are about to embark down a path of no return. The fact that you are creating a delimited list in a variable leads to believe you are hoping to use that in where clauses. If that is the case you are going to end up in another pickle....variables don't work like that.

    Perhaps you can explain what you are really trying to accomplish and we can help you find a better approach.

    You are right i did horrible job in explaining my question :(.

    The above query works but i went with a different solution.

    Two way street here... what is the solution that you did go for? It might help US in the future.

    Absolutely :). It was very simple, as Vijay mentioned i used coalesce function.

    DECLARE

    @database2 NVARCHAR(100)

    SET @database2 = NULL

    DECLARE @dbs TABLE ( DBname NVARCHAR(100) )

    INSERT INTO @dbs

    VALUES ( 'A' ),

    ( 'B' )

    ,

    ( 'C' )

    select @database2 = COALESCE(@database2 + ',', ' ')

    + DBname

    from @dbs

    select @database2