累加同列的内容

mmkkuoi 2012-11-28 11:15:13


create table test
(
t1 int ,
t2 nvarchar(50)
)


insert into test values(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'a'),(3,'c')

用function 返回 1的 a;b;c;
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
-Tracy-McGrady- 2012-11-28
  • 打赏
  • 举报
回复

select * into #myTest from test with(nolock)
alter #myTest add lj varchar(500)
declare @lj varchar(500)
set @lj=''
while exists (select top 1 lj from #myTest where lj is null)
begin
  select top(1000) * into #a from #myTest
  update #myTest set lj=lj+ddd fom #a where #a.t1<#myTest.t1
  drop table #a
  drop table #myTest
end

有错误,自己改吧,大概这样可以实现
SqlServer2008 2012-11-28
  • 打赏
  • 举报
回复

create function getT(@id int)
returns nvarchar(50)
begin
	declare @a nvarchar(50)
	select @a=''
	select @a=@a+t2+';' from test where t1=@id
	return @a
end

select dbo.getT(1)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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