SQL STUFF函數 拼接字串

來源:互聯網
上載者:User
文章目錄
  •  

 

     今日看到一篇文章,是關於和並列的,也研究了下,還是不錯的

 

 

 

 

 

要這種效果。

 

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

 

這樣即可。

 

 

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

收集的資料

[sql] view plaincopyprint?
  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 varchar) from 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  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.