• SQL STUFF函数 拼接字符串


     

         今日看到一篇文章,是关于和并列的,也研究了下,还是不错的

     

    要这种效果。

    create table tb(idint, value varchar(10))

    insert into tbvalues(1,'aa')

    insert into tbvalues(1,'bb')

    insert into tbvalues(2,'aaa')

    insert into tbvalues(2,'bbb')

    insert into tbvalues(2,'ccc')

    go

    /*         stuff(param1, startIndex, length, param2)
    说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用param2替换删掉的字符。*/

    SELECT id,
                          value = stuff
                              ((SELECT     ',' + value
                                  FROM         tb AS t
                                  WHERE     t .id = tb.id FOR xml path('')), 1, 1, '')
    FROM         tb
    GROUP BY id

    这样即可。

    ===================================================================================

    收集的资料

    1. /*  
    2. 标题:按某字段合并字符串之一(简单合并)  
    3. 作者:(十八年风雨,守得冰山雪莲花开)  
    4. 时间:2008-11-06  
    5. 地点:广东深圳  
    6.   
    7. 描述:将如下形式的数据按id字段合并value字段。  
    8. id    value  
    9. ----- ------   
    10. 1     aa  
    11. 1     bb  
    12. 2     aaa  
    13. 2     bbb  
    14. 2     ccc  
    15. 需要得到结果:  
    16. id     value  
    17. ------ -----------   
    18. 1      aa,bb  
    19. 2      aaa,bbb,ccc  
    20. 即:group by id, 求 value 的和(字符串相加)  
    21. */  
    22. --1、sql2000中只能用自定义的函数解决   
    23. create table tb(id int, value varchar(10))  
    24. insert into tb values(1, 'aa')  
    25. insert into tb values(1, 'bb')  
    26. insert into tb values(2, 'aaa')  
    27. insert into tb values(2, 'bbb')  
    28. insert into tb values(2, 'ccc')  
    29. go  
    30.   
    31. create function dbo.f_str(@id varchar(10)) returns varchar(1000)  
    32. as  
    33. begin  
    34.   declare @str varchar(1000)  
    35.   select @str = isnull(@str + ',' , '') + cast(value as varcharfrom tb where id = @id  
    36.   return @str  
    37. end  
    38. go  
    39.   
    40. --调用函数   
    41. select id , value = dbo.f_str(id) from tb group by id  
    42.   
    43. drop function dbo.f_str  
    44. drop table tb  
    45.   
    46.   
    47. --2、sql2005中的方法   
    48. create table tb(id int, value varchar(10))  
    49. insert into tb values(1, 'aa')  
    50. insert into tb values(1, 'bb')  
    51. insert into tb values(2, 'aaa')  
    52. insert into tb values(2, 'bbb')  
    53. insert into tb values(2, 'ccc')  
    54. go  
    55.   
    56. select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')  
    57. from tb  
    58. group by id  
    59.   
    60. drop table tb  
    61.   
    62.   
    63. --3、使用游标合并数据   
    64. create table tb(id int, value varchar(10))  
    65. insert into tb values(1, 'aa')  
    66. insert into tb values(1, 'bb')  
    67. insert into tb values(2, 'aaa')  
    68. insert into tb values(2, 'bbb')  
    69. insert into tb values(2, 'ccc')  
    70. go  
    71. declare @t table(id int,value varchar(100))--定义结果集表变量   
    72. --定义游标并进行合并处理   
    73. declare my_cursor cursor local for  
    74. select id , value from tb  
    75. declare @id_old int , @id int , @value varchar(10) , @s varchar(100)  
    76. open my_cursor  
    77. fetch my_cursor into @id , @value  
    78. select @id_old = @id , @s=''  
    79. while @@FETCH_STATUS = 0  
    80. begin  
    81.     if @id = @id_old  
    82.        select @s = @s + ',' + cast(@value as varchar)  
    83.     else  
    84.       begin  
    85.         insert @t values(@id_old , stuff(@s,1,1,''))  
    86.         select @s = ',' + cast(@value as varchar) , @id_old = @id  
    87.       end  
    88.     fetch my_cursor into @id , @value  
    89. END  
    90. insert @t values(@id_old , stuff(@s,1,1,''))  
    91. close my_cursor  
    92. deallocate my_cursor  
    93.   
    94. select * from @t  
    95. drop table tb  

  • 相关阅读:
    联系我们
    RCMTM _百度百科
    DotNetCore跨平台~Dockerfile的解释
    DotNetCore跨平台~服务总线_事件总线的重新设计
    DotNetCore跨平台~问题~NETCoreAPP, Version=v1.0' compatible with one of the target runtimes: 'win10-x64
    DotNetCore跨平台~EFCore连接Mysql的方式
    何时可以开启透明数据加密(TDE)?
    通信系统概论---传输介质
    struts2.x中因变量命名错误不被注入到值栈的问题
    【面向代码】学习 Deep Learning(三)Convolution Neural Network(CNN)
  • 原文地址:https://www.cnblogs.com/yongheng178/p/2565631.html
Copyright © 2020-2023  润新知