参考参考.呵呵.
if exists(select name from sysobjects
where name='procTest3' and type='p')
drop proc procTest3
go
create proc procTest3
As
declare @t_id int
declare @t_tm varchar(100)
declare @temp varchar(10)
--create table #t(id int,tm varchar(100))
declare cur1 cursor for (select [id] from table1 group by [id])
select @t_tm=''
open cur1
fetch next from cur1 into @t_id
while @@fetch_status=0
begin
declare cur2 cursor for (select tm from table1 where [id]=@t_id)
open cur2
fetch next from cur2 into @temp
while @@fetch_status=0
begin
select @t_tm=@t_tm+@temp+','
fetch next from cur2 into @temp
end
if right(@t_tm,1)=','
select @t_tm=left(@t_tm,len(@t_tm)-1)
print cast(@t_id as varchar(8))+' '+@t_tm
select @t_tm=''
close cur2
deallocate cur2
fetch next from cur1 into @t_id
end
close cur1
deallocate cur1