Hi Jeff,
Thanks for your reply and basically i am planning to create generic function which will take "table name","column name to makecomma separated" and "where condition" and send the result as comma separated values.
sample data to play with:
DEclare @sample Table(UserId int,name varchar(50))
insert into @sample
select 1, 'Test1' union all
select 2, 'Test1' union all
select 3, 'Test1' union all
select 4, 'Test1' union all
select 5, 'Test2' union all
select 6, 'Test3'
declare @Query nvarchar(max)
DECLARE @Names VARCHAR(8000)
Declare @tablename nvarchar(50) = '@sample',@columnname nvarchar(50) = 'userid',@Condition nvarchar = 'name = Test1'
SET @Query = ' SELECT ' + @Names + '= COALESCE( @Names + '', '', '''' ) + CAST( ' + @columnname + 'AS VARCHAR ) as value ' +
' FROM ' + @tablename +
' WHERE ' + @Condition
exec sp_executeSql @Query
select @Names
Expected output: 1,2,3,4,5,6
any suggestion please how to achieve this logic.