create table AggregationTable(Id int, [Name] varchar(10)) go insert into AggregationTable select 1,'赵' union all select 2,'钱' union all select 1,'孙' union all select 1,'李' union all select 2,'周' go
2.创建自定义字符串聚合函数
复制代码 代码如下:
Create FUNCTION AggregateString ( @Id int ) RETURNS varchar(1024) AS BEGIN declare @Str varchar(1024) set @Str = '' select @Str = @Str + [Name] from AggregationTable where [Id] = @Id return @Str END GO
3.执行下面的语句,并查看结果
复制代码 代码如下:
select dbo.AggregateString(Id),Id from AggregationTable group by Id