请问,SELECT查询到的结果如下:
1
2
3
4
如何能转为字符串格式,如:
1,2,3,4
只借助SQL及存储过程,不用其他语言

解决方案 »

  1.   


    go
    if OBJECT_ID('test')is not null
    drop table test
    go
    create table test(
    value int
    )
    insert test
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 declare @str varchar(20)
    set @str=''
    select @str=@str+','+ltrim(value) from test
    select RIGHT(@str,LEN(@str)-1) as value/*
    value
    1,2,3,4,5
    */
      

  2.   

    谢谢,找到方法了
    Select STUFF((Select ','+RoleName  FROM tbRole WHERE ID IN (SELECT RoleID FROM tbAdminRole WHERE AdminID = 9) FOR XML PATH('')),1,1,'');
    主要是想实现在网页上显示的管理员列表里,显示出每个管理员所属的多个角色,上面的整个语句还需要作为一个子查询放在管理员查询语句里