--测试数据
create table csdn(id int,txt varchar(10))
insert csdn
select 1, 'a ' union all
select 1, 'b ' union all
select 1, 'c ' union all
select 2, 'aa ' union all
select 2, 'bb ' union all
select 2, 'cc ' union all
select 3, 'aaa ' union all
select 3, 'bbb '
select * from csdn
go
create function Gettxt(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s= ' '
select @s=@s + ', ' +txt from csdn where id=@id
--return @s
return stuff(@s,1,1, ' ')
end
go
select id,dbo.Gettxt(id) txt from csdn group by id
go
drop function Gettxt
drop table csdn
go
贴子来源飞诺网( http://bbs.firnow.com) 详细出处参考: http://bbs.firnow.com/dview6t82595.html
create table csdn(id int,txt varchar(10))
insert csdn
select 1, 'a ' union all
select 1, 'b ' union all
select 1, 'c ' union all
select 2, 'aa ' union all
select 2, 'bb ' union all
select 2, 'cc ' union all
select 3, 'aaa ' union all
select 3, 'bbb '
select * from csdn
go
create function Gettxt(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s= ' '
select @s=@s + ', ' +txt from csdn where id=@id
--return @s
return stuff(@s,1,1, ' ')
end
go
select id,dbo.Gettxt(id) txt from csdn group by id
go
drop function Gettxt
drop table csdn
go
贴子来源飞诺网( http://bbs.firnow.com) 详细出处参考: http://bbs.firnow.com/dview6t82595.html
本文介绍了一种使用SQL函数来实现字符串拼接的方法。通过创建一个存储过程,该过程可以接收一个整数ID作为参数,并返回与该ID关联的所有记录的字符串值,这些字符串值以逗号分隔。此外,还演示了如何使用stuff函数去除首部的逗号。
1845

被折叠的 条评论
为什么被折叠?



