declare @varLocID varchar(100)
select @varLocID=isnull(@varLocID,'') + locid +'+' from msgloc where msgid='0002'select msgid ,@varLocID as locid ,msgtext from msg

解决方案 »

  1.   

    --测试--测试数据
    create table t1(a1 int,a2 varchar(10))
    insert t1 select 1,'one'
    union all select 2,'two'
    union all select 3,'three'
    union all select 1,'one1'
    union all select 2,'twot'
    union all select 3,'threex'
    union all select 3,'dog'
    go--创建处理函数
    create function f_str(@a1 int)
    returns varchar(8000)
    as
    begin
    declare @re varchar(8000)
    set @re=''
    select @re=@re+'/'+a2 from t1 where a1=@a1
    return(stuff(@re,1,1,''))
    end
    go--调用实现查询
    select a1,a3=dbo.f_str(a1)
    from t1
    group by a1
    godrop table t1
    drop function f_str/*--测试结果表明
    a1          a3                   
    ----------- ---------------------
    1           one/one1
    2           two/twot
    3           three/threex/dog(所影响的行数为 3 行)
    --*/
      

  2.   

    参考
    --生成
    F1       F2                                  F1       F2
    --------------------                ------------------------------
    a        c                                   a       c,d,e
    a        d                =======>           b       f,g,h
    a        e
    b        f
    b        g
    b        h--示例数据
    create table tb(F1 varchar(10),F2 varchar(10))
    insert tb select 'a','c'
    union all select 'a','d'
    union all select 'a','e'
    union all select 'b','f'
    union all select 'b','g'
    union all select 'b','h'
    go--用一条sql语句实现(实际是没有一条sql语句实现的,这里是取巧)
    exec('set nocount on
    select f1,f2=cast(f2 as varchar(8000)) into #t from tb order by f1
    declare @f1 varchar(10),@r varchar(8000)
    update #t set @r=case f1 when @f1 then @r+'',''+f2 else f2 end,f2=@r,@f1=f1
    select F1,F2=max(f2) from #t group by f1
    ')
    go