我想,应该要用游标或者临时表!
给个不用游标的方法:select * into #Temp from tablename
select xm,convert(varchar(200),min(qdh)) as qdh into #Result from #Temp
group by xmdelete #Temp
from #Temp t,#Result r
where t.xm=r.xm
and t.qdh=r.qdhwhile exists (select * from #Temp)
begin
select xm,convert(varchar(20),min(qdh)) as qdh
into #Temp1
from #Temp
group by xm
update #Result
set qdh=r.qdh+','+t.qdh
from #Result r,#Temp1 t
where r.xm=t.xm
delete #Temp
from #Temp t,#Temp1 r
where t.xm=r.xm
and t.qdh=r.qdh
drop table #Temp1
enddrop table #Temp
select * from #Result
给个不用游标的方法:select * into #Temp from tablename
select xm,convert(varchar(200),min(qdh)) as qdh into #Result from #Temp
group by xmdelete #Temp
from #Temp t,#Result r
where t.xm=r.xm
and t.qdh=r.qdhwhile exists (select * from #Temp)
begin
select xm,convert(varchar(20),min(qdh)) as qdh
into #Temp1
from #Temp
group by xm
update #Result
set qdh=r.qdh+','+t.qdh
from #Result r,#Temp1 t
where r.xm=t.xm
delete #Temp
from #Temp t,#Temp1 r
where t.xm=r.xm
and t.qdh=r.qdh
drop table #Temp1
enddrop table #Temp
select * from #Result
select distinct xm from Tablename for read only
declare @Result table (xm varchar(20),qdh varchar(200))
declare @xm varchar(20)
declare @qdh varchar(200)open cur
fetch next from cur into @xm
while( @@fetch_status =0 )
begin
set @qdh=''
select @qdh=@qdh+','+qdh from TableName where xm=@xm
update @Result set qdh=substring(@qdh,2,len(@qdh)-1)
where xm=@xm fetch next from cur into @dep,@pono
end
close cur
deallocate curselect * from @Result
select distinct xm from Tablename for read only
declare @Result table (xm varchar(20),qdh varchar(200))
declare @xm varchar(20)
declare @qdh varchar(200)
open cur
fetch next from cur into @xm
while( @@fetch_status =0 )
begin
set @qdh=''
select @qdh=@qdh+','+qdh from TableName where xm=@xm
update @Result set qdh=substring(@qdh,2,len(@qdh)-1)
where xm=@xm
fetch next from cur into @xm
end
close cur
deallocate cur
select * from @Result