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

posted on 2015-04-13 18:18  蒋乐兴的技术随笔  阅读(132)  评论(0编辑  收藏  举报

导航