create table #t(id int, name varchar(100))insert into #t select '001','张三' union all select '002','李四' union all select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + ',' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三,李四,老五
declare @tab table(id1 varchar(10),name1 varchar(20))insert @tab values('001','张三') insert @tab values('002','李四') insert @tab values('003','老五')select * from @tabdeclare @str varchar(5000) set @str=''select @str=@str+name1+'、' from @tabselect @str
create table #t(id int, name varchar(100))insert into #t select '001','张三' union all select '002','李四' union all select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + '、' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三、李四、老五
select '001','张三' union all
select '002','李四' union all
select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + ',' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三,李四,老五
insert @tab values('002','李四')
insert @tab values('003','老五')select * from @tabdeclare @str varchar(5000)
set @str=''select @str=@str+name1+'、' from @tabselect @str
select '001','张三' union all
select '002','李四' union all
select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + '、' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三、李四、老五