62,046
社区成员
发帖
与我相关
我的任务
分享
Create table 数据 --创建表
(
ID Int IDENTITY(1,1) not null,
编号 Varchar(50),
明细 Varchar(50),
Primary Key(ID)
);
Insert Into 数据
Select 1,'a' Union all
Select 1,'b' Union all
Select 1,'c' Union all
Select 2,'a' Union all
Select 2,'b' Union all
Select 2,'d'Union all
Select 3,'a'Union all
Select 3,'b'
--创建自定义函数
Create Function CSDN
(
@mx Varchar(50)
)
Returns Varchar(8000)
as
Begin
Declare @str Varchar(8000)
Set @str = ''
Select @str = @str + cast(明细 as Varchar(50)) + ',' from 数据 Where 编号 = @mx
Set @str = SubString(@str,1,len(@str)-1)
Return(@str)
End
--调用自定义函数得到结果
Select Distinct 编号,dbo.CSDN(编号) as 明细From 数据;