dynamically creating query in a loop
Assuming your pseudo-code is a good approximation of what your actual code is doing, you'd need something like:

DECLARE @sql NVARCHAR(MAX);
SET @sql = '';
WHILE (Data is in the table)
BEGIN
    SELECT TOP 1 @result = result from table;

    @sql = @sql + ', ' + @result;

END


If you don't initialize @sql to a zero-length string prior to the loop, SQL Server will attempt to append @result to a NULL string, which results in a NULL string, hence the @sql at the end of the loop will appear empty.