MS SQL While循环代替游标

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循环代替游标

MS SQL While循环代替游标,布布扣,bubuko.com

MS SQL While循环代替游标

上一篇:PowerDesigner15.1创建模型及生成带注释sql操作手册


下一篇:数据库中自动以函数用法