日期:2014-05-17 浏览次数:20621 次
declare @tab table
(
id int,
name nvarchar(50),
type nvarchar(50)
)
insert into @tab(id,name,type)
select 1,'北京','综合'
union all
select 1,'北京','专业'
union all
select 2,'天津','专业'
union all
select 2,'天津','综合'
select id,name,type=(select type+',' from @tab where id=t.id and name=t.name for xml path('')) from @tab t group by id,name
create table t(id int ,name varchar(50),type varchar(100))
insert into t
select 1,'北京','综合' union all
select 1,'北京','专业' union all
select 2,'天津','专业' union all
select 2,'天津','综合' union all
select distinct id,name,type=(select ','+type from t b where a.id=b.id and a.name=b.name for xml path('')) from t a
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@id int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+type from Tb where id=@id
return @S
end
go
Select distinct id,name,type=dbo.F_Str(Col1) from Tb
go
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@id int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+type from Tb where id=@id
return @S
end
go
Select distinct id,name,type=dbo.F_Str(type) from Tb
go