create table persons(Name nvarchar(32) not null); go
insert into persons(Name) values('张三'),('李四'),('王五'); go
declare @i as int =1;--循环计数器 declare @rows as int;--循环结束标记 declare @Names nvarchar(max)='';--用于保存结果的变量
select @rows = count(*) from persons; select Name,ROW_NUMBER() over(order by Name) as ID into #t from persons;
while @i<= @rows begin select @Names = @Names + name+' ,' from #t as a where a.ID = @i; set @i = @i +1; end
select left(@Names,len(@Names)-1) as 姓名列表;
drop table #t; go