34,588
社区成员
发帖
与我相关
我的任务
分享
--创建测试表
CREATE TABLE t(Name VARCHAR(10),Score INT)
--插入测试数据
insert into t
select '张三',100 union all
select '李四',200 union all
select '王五',390
declare @i int
,@count int
,@Name varchar(10)
--表记录总数
select @count = count(1) from t
--初始化循环变量
set @i = 1
select row_number() over(order by Name) ID,* into #t from t
while @i <= @count
begin
select @Name = Name from #t where ID = @i
print @Name
--改变循环变量
set @i = @i + 1
end
WHILE EXISTS(select [id] from @temp)
begin
select top 1 @tempId = [id],@tempName=[Name] from @temp
delete from @temp where [id] = @tempId
print 'Name:----'+@tempName
end