group by下同一列字符串链接函数及sql(sql server 2000下)[转]

来源:互联网 发布:图像识别软件价格 编辑:程序博客网 时间:2024/06/03 16:04

 create table tb(id int,txt varchar(100))  
  go  
  insert into tb  
  select 1,'aaa' union all  
  select 1,'bbb' union all  
  select 2,'ccc' union all  
  select 3,'ddd' union all  
  select 3,'eee' union all  
  select 3,'fff'  
   
  go  
  --写一个聚合函数:  
  create function dbo.fn_Merge(@id int)  
  returns varchar(8000)  
  as  
  begin  
  declare @r varchar(8000)  
  set @r=''  
  select @r=@r+';'+txt from tb where id=@id  
  return stuff(@r,1,1,'')  
  end  
  go  
   
  -- 调用函数  
  select id, dbo.fn_Merge(id) as txt from tb group by id  
   
  go  
  drop table tb  
  drop function fn_Merge
这条语句在2005是能执行出来的,可是2000里确不行,大家给看看,大概意思就是,我要得到 下面的结果
id txt  
  1 aaa  
  1 bbb  
  2 ccc  
  3 ddd  
  3 eee  
  3 fff  
   
   
  select id,***(txt,';') from tb group by id  
  结果:  
  1 aaa;bbb  
  2 ccc  
  3 ddd;eee;fff

 

转自:http://topic.csdn.net/u/20100520/15/176afe53-0a0c-40cd-86ac-c2a3a3b41130.html

原创粉丝点击