读书人

急问!字符串列相加如何实现

发布时间: 2012-02-19 19:43:39 作者: rapoo

急问!字符串列相加怎么实现!
ID STATUS
49 Y1
49 Y2
50 Y3
49 Y4

ID 49 相加得到 Y1Y2Y4

怎么实现 谢谢!!

[解决办法]
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
end

select id,dbo.aa(id) from t group by id

id
----------- ----------------------------------------------------------------
49 Y1Y2Y4
50 Y3

(2 row(s) affected)

读书人网 >SQL Server

热点推荐