日期:2014-05-18 浏览次数:20408 次
--创建环境 create table test(iID int,content varchar(100)) insert into test select 100,'a' union select 100,'b' union select 101,'c' union select 101,'d' union select 101,'e' --创建一个合并的函数 create function contentDo(@id int) returns varchar(8000) as begin declare @str varchar(8000) set @str='' select @str=@str+','+cast(content as varchar) from test where iID=@id set @str=right(@str,len(@str)-1) return(@str) End go --调用自定义函数得到结果 select iID,max(dbo.contentDo(iID)) as content from test group by iID --查询结果 iID content ------------ 100 a,b 101 c,d,e