sql server多行數(shù)據(jù)拼接的實(shí)例方法
發(fā)布日期:2022-01-03 03:21 | 文章來源:站長之家
idtypeproductCode
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
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信