create table 表名(id varchar(10),name varchar(10),tel varchar(100))
goinsert into 表名 select 1,'a','123'
insert into 表名 select 2,'a','432'
insert into 表名 select 3,'a','990'
insert into 表名 select 4,'b','744'
insert into 表名 select 5,'b','444'
insert into 表名 select 6,'b','323'
go--创建一个合并的函数
create function f_merge(@name varchar(100))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + ',' + cast(tel as varchar(100)) from 表名 where name = @name
  set @str = stuff(@str , 1,1,'')
  return(@str)
End
goselect * from 表名--调用自定义函数得到结果:
select name ,dbo.f_merge(name) as tel from 表名 group by namedrop table 表名
drop function f_merge
/*name    tel
 a    123,432,990
 b    744,444,323*/

解决方案 »

  1.   


    SQL Server 2000中可以通过两种方式处理这种问题:1、创建自定义函数,如上所述。2、用游标。使用函数处理是最简单的方式。
      

  2.   

    create table table1(id int,name varchar(10),tel varchar(100))insert into table1 select 1,'a',123
    insert into table1 select 2,'a',432
    insert into table1 select 3,'a',990
    insert into table1 select 4,'b',744
    insert into table1 select 5,'b',444
    insert into table1 select 6,'b',323
    --用游标
    declare @ta table(name varchar(10),tel varchar(1000))
    declare roy_cursor cursor for
    select name,tel from table1 order by id
    declare @name varchar(10),@tel int,@s varchar(1000),@name1 varchar(10)
    open roy_cursor
    fetch next from roy_cursor into @name,@tel
    SELECT @name1=@name,@s=''
    while @@fetch_status=0
    begin
    if @name=@name1
    select @s=@s+','+rtrim(@tel)
    else
    begin
    insert @ta values(@name1,stuff(@s,1,1,''))
    select @s=','+rtrim(@tel),@name1=@name
    end
    fetch next from roy_cursor into @name,@tel
    end
    insert @ta values(@name1,stuff(@s,1,1,''))
    close roy_cursor
    deallocate roy_cursorselect * from @ta(1 行受影响)(1 行受影响)
    name       tel
    ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    a          123,432,990
    b          744,444,323(2 行受影响)
      

  3.   

    create table table1(id int,name varchar(10),tel varchar(100))insert into table1 select 1,'a',123
    insert into table1 select 2,'a',432
    insert into table1 select 3,'a',990
    insert into table1 select 4,'b',744
    insert into table1 select 5,'b',444
    insert into table1 select 6,'b',323alter table table1 add 显示列 varchar(2000) null--新列
    --用游标
    go
    declare @ta table(id int,name varchar(10),tel int)
    insert @ta select id,name,tel from table1
    while exists(select 1 from @ta )
    begin
    update a
    set 显示列=isnull(显示列+',','')+rtrim(b.tel)
    from table1 a join @ta b on a.name=b.name
    where not exists(select 1 from @ta where name=b.name and id<b.id)
    delete b
    from @ta b
    where not exists(select 1 from @ta where name=b.name and id<b.id)
    end
    go
    select distinct name,显示列 from table1
    go
    --删除新增列
    alter table table1 drop column 显示列
    name       显示列
    ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    a          123,432,990
    b          744,444,323(2 行受影响)