当前位置:Gxlcms > 数据库问题 > 如何将sql查询出的结果,用符号隔开

如何将sql查询出的结果,用符号隔开

时间:2021-07-01 10:21:17 帮助过:119人阅读

1 100 1 101 1 106 2 100 2 109 3 112 如何转换为 1 100,101,106 2 100,109 3 112

知道写存储过程或者函数可以解决,但是想想能不能用一条sql语句解决...未果...

还是去搜索了下怎么搞,→转载链接←

create table tb(id int, value varchar(10))
insert into tb values(1, aa)
insert into tb values(1, bb)
insert into tb values(2, aaa)
insert into tb values(2, bbb)
insert into tb values(2, ccc)
go
create function [dbo].[f_str](@id int) returns nvarchar(1000)
as
begin
declare @str nvarchar(1000)
set @str = ‘‘
select @str = @str + , + cast(value as nvarchar(900)) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go
--调用函数
select id , value = dbo.f_str(id) from tb group by id 

 

如何将sql查询出的结果,用符号隔开

标签:create   存储过程   问题   end   http   value   color   sel   搜索   

人气教程排行