create function f_c(@buyid varchar(10))
returns varchar(8000)
as 
begin
declare @char varchar(8000)
set @char=''
select @char=@char+clientname+' ' from tb where buyid=@buyid
return left(@char,len(@char)-1)
endgocreate function f_t(@buyid varchar(10))
returns varchar(8000)
as 
begin
declare @char varchar(8000)
set @char=''
select @char=@char+tel+' ' from tb where buyid=@buyid
return left(@char,len(@char)-1)
endgoselect distinct BuyId,dbo.f_c(BuyId),dbo.f_t(BuyId) from tb

解决方案 »

  1.   

    create table t
    (BuyId int,clientName varchar(20),tel varchar(50))
    insert t
    select 24,'xu','123,124,111' union all
    select 24,'zhang','1110  135  111' union all
    select 24,'张山','010-121212'
    go
    create function f_he(@id int,@col int)
    returns varchar(100)
    as
    begin
       declare @sql varchar(100)
       set @sql=''
     if @col=0
       select @sql=@sql+' '+tel from t where BuyId=@id
      else 
       select @sql=@sql+' '+clientName from t where BuyId=@id
      return(stuff(@sql,1,1,''))
    end
    goselect BuyId,dbo.f_he(BuyId,1) as clientName,dbo.f_he(BuyId,0) as tel from t group by BuyIddrop table t
    drop function f_he