I have a T-SQL function to return all the associated columns of the statistics attribute I am passing the stats_id and object_id of sys.stats system table. I need all the columns separated by ',' but my function returns just the first column.Pls help I am working on Sql server 2005
create FUNCTION fGetstatscols (
@objectid INT,
@stats_id INT
)
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @V_IncludedCol NVARCHAR(200)
DECLARE collist CURSOR
( select (name) from sys.columns where Object_id =@objectid
and column_id in (select column_id from sys.stats_columns where object_id=@objectid
and stats_id=@stats_id))
OPEN collist
FETCH NEXT FROM collist INTO @V_IncludedCol
WHILE @@FETCH_STATUS <> 0
begin
Select @V_IncludedCol = COALESCE(@V_IncludedCol + ',', '')
END
close collist
deallocate collist
RETURN @V_IncludedCol
END