MS SQL While循环代替游标

--create temp1 table
create table #Grade(
ID int identity(1,2),
Name varchar(20),
SubjectName varchar(20),
Grade int
)
--insert data
insert into #Grade(Name,SubjectName,Grade) values(jimmy,Math,100)
insert into #Grade(Name,SubjectName,Grade) values(jimmy,Chinese,110)
insert into #Grade(Name,SubjectName,Grade) values(jimmy,English,90)
insert into #Grade(Name,SubjectName,Grade) values(tom,Math,60)
insert into #Grade(Name,SubjectName,Grade) values(tom,Chinese,80)
insert into #Grade(Name,SubjectName,Grade) values(tom,English,90)
insert into #Grade(Name,SubjectName,Grade) values(lina,Math,110)
insert into #Grade(Name,SubjectName,Grade) values(lina,Chinese,120)
insert into #Grade(Name,SubjectName,Grade) values(lina,English,50)

--create and insert temp2 table
create table #Temp(
tID int identity(1,1),
ID int)
insert into #Temp (ID) select ID from #Grade 

--set para
declare @begin int 
declare @count int 
declare @ID int
set @begin=1
set @count=(select COUNT(*) from #Temp)

--doing something
while(@begin<=@count)
begin
    set @ID=(select ID from #Temp where tID=@begin)
    update #Grade set Name=Name+ updated  where ID=@ID
    set @begin=@begin+1
end

select * from #Grade

--drop temp table
drop table #Temp
--drop temp table
drop table #Grade

MS SQL While循环代替游标,古老的榕树,5-wow.com

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