多行变一行的问题

ask999 2008-10-07 01:00:00
原来的表如下:
1 a
1 b
1 c
2 d
2 e
2 f
现在查询后希望变为
1 a,b,c
2 d,e,f
用ADO+vb+sql2000
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianzy 2008-10-08
  • 打赏
  • 举报
回复

create table tb(id int,name varchar(100))
insert into tb(id,name) values (1,'a')
insert into tb(id,name) values (1,'b')
insert into tb(id,name) values (1,'c')
insert into tb(id,name) values (2,'d')
insert into tb(id,name) values (2,'e')
insert into tb(id,name) values (2,'f')

create function getStr(@id as int)
returns varchar(4000)
as
begin
declare @s as varchar(4000)
set @s=''
select @s=@s+ ',' + name from tb where id=@id
return stuff(@s,1,1,'')
end

select id,dbo.getStr(id) as name from tb group by id
dlpseeyou 2008-10-07
  • 打赏
  • 举报
回复
create Function Gets_sh(@id as varchar(30))
Returns Varchar(1000)
As
Begin
Declare @S Varchar(1000)

Set @S=''
Select @S=@S+','+ sh from table where id=@id
Return (Stuff(@S,1,1,''))
End

--select gets_sh(id) from table
ask999 2008-10-07
  • 打赏
  • 举报
回复
存储过程怎么弄?分不够再加100哦。
vbman2003 2008-10-07
  • 打赏
  • 举报
回复
用存储过解决,SQL语句没法弄

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧