Loop in sql

Webbo_1980 used Ask the Experts™
Lets say i wanted to build a string value up, based on the contents of a table e.g.

select * from tableA

would return results such as

ID | Name
1    bob  
2    tim    
3    Ed      

And say i wanted to populare a variable which loops through each row and stores/concatinates it as e.g.


How would i do this?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Consultant
Top Expert 2009
declare @String varchar(max)
select @String = coalesce (@string+'', '') + Name from yourTable
select @String

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial