This is an example of generating a comma-delimited string of values in SQL. The items are originally stored as separate rows in a table of the format
ID PID Project Type
1 200 Really Fun
2 200 Kinda Fun
3 200 Wildy Fun
4 201 Not Fun
The result is
PID | Project Description | Project Types
200 | Some Fun Project | Really Fun, Kinda Fun, Wildy Fun
What we want, in other words, are all project types located in another table, shown as a single varchar() field. "Really Fun, Kinda Fun, Wildy Fun"
declare pidcursor CURSOR
GLOBAL
for select pid, project_type from v_projtypes order by pid
open pidcursor
declare @project_type varchar(255)
select @totalcount = count(*) from v_projtypes
select @counter = 1
fetch next from pidcursor into @pidchk, @project_type
while (@counter <= @totalcount) and (@@fetch_status=0)
begin
update #tmp_projtypes set project_types = project_types + ', ' + @project_type where pid = @pidchk
fetch next from pidcursor into @pidchk, @project_type
end
close pidcursor
deallocate pidcursor