declare @s varchar(2000)
set @s=''
select @s=@s+','+cast(Value as varchar(10))+'' from 表 where TypeId=2
select stuff(@s,1,1,'')

解决方案 »

  1.   

    --Sorr,','换成'|'
    declare @s varchar(2000)
    set @s=''
    select @s=@s+'|'+cast(Value as varchar(10))+'' from 表 where TypeId=2
    select stuff(@s,1,1,'')
      

  2.   

    --测试环境,写个函数很方便返回字符串
    create table T(id int identity(1,1),Value int,TypeId int)
    insert into T select 12345,1
    union all select 165699,2
    union all select 52654,2
    union all select 526254,2
    union all select 526254,3Create function F_Getvalue(@TypeId int)
    returns varchar(2000)
    as
    begin
    declare @return varchar(2000)
    set @return=''
    select @return=@return+'|'+cast(Value as varchar(10))+'' from T where TypeId =@TypeId 
    return stuff(@return,1,1,'')
    end--测试(传递参数就可以了)
    select dbo.F_Getvalue(2)--结果
    165699|52654|526254