There is no need to resort to looping for this. You can instead use STUFF.
Thanks Mark for the ddl and sample data.
declare @a table (Linenumber int, ErrorText varchar(30),FileID int)
insert into @a values
(1, 'a', 10),
(1, 'b', 10),
(2, 'a', 11),
(2, 'b', 11),
(2, 'c', 11),
(3, 'c', 12),
(3, 'd', 12)
select Linenumber,
STUFF((select ',' + ErrorText
from @a a2
where a1.Linenumber = a2.Linenumber
order by a2.ErrorText
for xml path('')), 1, 1, '') as ErrorTexts
, FileID
from @a a1
group by Linenumber, FileID
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/