日期:2014-05-20 浏览次数:20842 次
Create table T1 --创建表
(
ID Int not null primary key identity(1,1),
Name Varchar(50),
Type Varchar(50),
);
Insert Into T1
Select 'A','S' Union all
Select 'B','S' Union all
Select 'C','S' Union all
Select 'D','E' Union all
Select 'E','E'
--创建自定义函数
Create Function CSDN_T1
(
@mx Varchar(50)
)
Returns Varchar(8000)
as
Begin
Declare @str Varchar(8000)
Set @str = ''
Select @str = @str + cast(Name as Varchar(50)) + ';' from T1 Where [Type] = @mx
Set @str = SubString(@str,1,len(@str)-1)
Return(@str)
End
--调用自定义函数得到结果
select Distinct Type,dbo.CSDN_T1(Type) as Name From T1 ;
with t as
(
select (select Name+',' from table where Type='M' for xml path('')) as Name
)
select LEFT(name,LEN(name)-1) from t