欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

sql server多行數(shù)據(jù)拼接的實(shí)例方法

 更新時間:2013年04月26日 15:40:42   作者:  
sql server多行數(shù)據(jù)拼接的實(shí)例方法,需要的朋友可以參考一下
1.表結(jié)構(gòu)
id type productCode
1 鉛筆 0001
2 鉛筆 0002
3 鉛筆 0003
4 鋼筆 0004
5 鋼筆 0005
6 鋼筆 0004
7 圓珠筆 0007
8 圓珠筆 0008
9 圓珠筆 0007
2.自定義函數(shù)fun
復(fù)制代碼 代碼如下:

GO
/****** Object:  UserDefinedFunction [dbo].[fun]    Script Date: 11/22/2011 16:09:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create function [dbo].[fun](@type nvarchar(10))
returns nvarchar(200)
as
begin
    declare @re nvarchar(200)
    declare @code nvarchar(200)
    set @re=''
    set @code=''
    select @re=@re+productCode+',' from T where type=@type group by productCode
    select @re=left(@re, len(@re)-1)
    return @re
end

3.查詢語句
select type,dbo.fun(type) from (select distinct type from T) A
結(jié)果:
鋼筆   0004,0005
鉛筆   0001,0002,0003
圓珠筆   0007,0008

相關(guān)文章

最新評論