SQL语句实现结果集合的拼接

Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
Go

create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin    
declare @S nvarchar(100)   
select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1    
return @S
end
go
Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
go

posted on 2008-06-22 21:41  心悦  阅读(1112)  评论(0编辑  收藏  举报