ID STATUS 49 Y1 49 Y2 50 Y3 49 Y4ID 49 相加得到 Y1Y2Y4------------- create funcion aa(@id int) returns varchar(100) as begin declare @s varchar(100) set @s='' select @s=@s+name from t where id=@id return @s endselect id,dbo.aa(id) from t
create table t (ID id, STATUS varchar(10)) insert into t select 49, 'Y1' union all select 49, 'Y2' union all select 50, 'Y3' union all select 49, 'Y4'create function aa(@id int) returns varchar(100) as begin declare @s varchar(100) set @s='' select @s=@s+status from t where id=@id return @s endselect id,dbo.aa(id) from t group by idid ----------- ---------------------------------------------------------------------------------------------------- 49 Y1Y2Y4 50 Y3(2 row(s) affected)
不好意思,int型 但我的確定義是id ,它被定義為int 型
create table t (ID int, STATUS varchar(10)) insert into t select 49, 'Y1' union all select 49, 'Y2' union all select 50, 'Y3' union all select 49, 'Y4'create function aa(@id int) returns varchar(100) as begin declare @s varchar(100) set @s='' select @s=@s+isnull(status,'') from t where id=@id return @s endselect id,dbo.aa(id) from t group by idid ----------- ---------------------------------------------------------------------------------------------------- 49 Y1Y2Y4 50 Y3(2 row(s) affected)
49 Y1
49 Y2
50 Y3
49 Y4ID 49 相加得到 Y1Y2Y4-------------
create funcion aa(@id int)
returns varchar(100)
as
begin
declare @s varchar(100)
set @s=''
select @s=@s+name from t where id=@id
return @s
endselect id,dbo.aa(id) from t
(ID id, STATUS varchar(10))
insert into t
select 49, 'Y1' union all
select 49, 'Y2' union all
select 50, 'Y3' union all
select 49, 'Y4'create function aa(@id int)
returns varchar(100)
as
begin
declare @s varchar(100)
set @s=''
select @s=@s+status from t where id=@id
return @s
endselect id,dbo.aa(id) from t group by idid
----------- ----------------------------------------------------------------------------------------------------
49 Y1Y2Y4
50 Y3(2 row(s) affected)
但我的確定義是id ,它被定義為int 型
(ID int, STATUS varchar(10))
insert into t
select 49, 'Y1' union all
select 49, 'Y2' union all
select 50, 'Y3' union all
select 49, 'Y4'create function aa(@id int)
returns varchar(100)
as
begin
declare @s varchar(100)
set @s=''
select @s=@s+isnull(status,'') from t where id=@id
return @s
endselect id,dbo.aa(id) from t group by idid
----------- ----------------------------------------------------------------------------------------------------
49 Y1Y2Y4
50 Y3(2 row(s) affected)