一个表
select '001' as [id],'aaa' as [desc]
into #t1
union
select '001','bbb'
union
select '002','ccc'
union
select '002','ddd'
union
select '003','eee'
union
select '004','fff'想要形成以下形式的数据集
001 aaa,bbb
002 ccc,ddd
003 eee
004 fff
如果不考虑用function,如何做?

解决方案 »

  1.   

    听别人说SQL SERVER 2005可以不用函数,2000一定要用函数才行的~~~
      

  2.   

    2000  没见过可以直接用T-sql语句的
      

  3.   

    --比如....
    select '001' as [id],'aaa' as [desc]
    into #t1
    union
    select '001','bbb'
    union
    select '002','ccc'
    union
    select '002','ddd'
    union
    select '003','eee'
    union
    select '004','fff'create table #temp ([id] varchar(03),[desc] varchar(100))
    declare @id varchar(03),@desc varchar(03)
    declare c1 cursor for 
    select * from #t1 order by [id]
    open c1
    fetch next from c1 into @id,@desc
    while @@fetch_status=0
    begin
       if exists (select 1 from #temp where [id]=@id)
           begin
             update #temp set [desc]=[desc]+','+@desc where [id]=@id
            end
      else
           begin
             insert into #temp ([id],[desc])  values (@id,@desc)
           end
      fetch next from c1 into @id,@desc
    end
    close c1
    deallocate c1select * from #tempdrop table #t1
    drop table #temp/*結果*/
    id   desc   
    ---------------------
    001  aaa,bbb
    002  ccc,ddd
    003  eee
    004  fff
      

  4.   

    写一个function返回字符串的,传递参数是id