SQL Server

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。