[CODE]create table #a
(
col int,
col_1 varchar(2),
col_2 varchar(2),
col_3 varchar(2),
)
insert into #a values (1,'a1','b1','c1')
insert into #a values (1,'a1','b1','c2')
insert into #a values (2,'a2','b2','c1')
insert into #a values (2,'a2','b2','c2')
insert into #a values (2,'a2','b2','c3')
create table #result
(
col int,
col_1 varchar(2),
col_2 varchar(2),
col_3 varchar(100),
)
delete #result
declare @col int,@col_1 varchar(2),@col_2 varchar(2),@col_3 varchar(2)
declare a_cursor cursor
for
Select col,col_1,col_2,col_3 from #a
open a_cursor
fetch next from a_cursor into @col,@col_1,@col_2,@col_3
while @@fetch_status=0
begin
if not exists(select * from #result where col=@col and col_1=@col_1 and col_2=@col_2)
begin
insert into #result values (@col,@col_1,@col_2,@col_3)
end
else
begin
update #result set col_3=col_3+','+@col_3 where col=@col and col_1=@col_1 and col_2=@col_2
end
fetch next from a_cursor into @col,@col_1,@col_2,@col_3
end
close a_cursor
deallocate a_cursor
SELECT * FROM #result[/CODE]