CREATE FUNCTION SchemaName.GetValuesCommaSeparated
(
@Id int
)
RETURNS varchar(100) -- or whatever length you need
AS
BEGIN
declare @values as varchar(100)
declare @result as varchar(100) =''
DECLARE cursorValues CURSOR
FOR
select T.ColumnName from SchemaName.TableName T where T.ID = @Id
--OPEN CURSOR.
OPEN cursorValues
--FETCH THE RECORD INTO THE VARIABLES.
FETCH NEXT FROM cursorValues INTO @values
--LOOP UNTIL RECORDS ARE AVAILABLE.
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @result += @values +','
FETCH NEXT FROM cursorValues INTO @values
END
--CLOSE THE CURSOR.
CLOSE cursorValues
DEALLOCATE cursorValues
RETURN @result;
END
declare @a as varchar(100)
set @a = SchemaName.GetValuesCommaSeparated(5)
print @a