一道SQLserver流程控制题

来源:百度知道 编辑:UC知道 时间:2024/09/23 01:27:18
用while continue break语句实现
提分(有不及格的就加分,直到全部及格 )
declare @score int
select @score= score from t_score
while @score<60
begin
update t_score
set score=@score+5
if @score>=60
break
else
continue
end
select * from t_score
哪里不对啊?
不要说这样编复杂,是老师刚讲过while continue break语句,为巩固出的习题。

这是死循环
select @score= score from t_score
这条是将所有的score都赋值给@score 没有这样进行处理的,你应该取一条一条的进行处理,

“>=”号在SQLserver里面是无效的 要分开写 用and连接

看你的程序,两种结果:不循环和死循环。因为@score没有变化。其实更新个成绩不需要搞的这么复杂,还用到循环?好玩哪?

update t_score set score=60 where score<60

======================================================
写程序,首先是写给人看的,其次才是机器。
代码应该考虑简洁明了,执行效率
======================================================
只能说你老师出题的水平太不上道,非要搞的蛇用脚走路不可,哎,不服都不行,搞个自然数求和也比这个好,
假定你成绩表的学生编号是stu_no

select identity(int,1,1) flag ,stu_no,score into #tmp from t_score

declare @a int ,@b int,@c int

set @a=1
select @b=max(flag) from #tmp

while @a<=@b
begin
select @c=score from #tmp where flag=@a
if @c<60
begin
update t_score set score=60 from #tmp a,t_score b where a.flag=@a and a.stu_no=b.stu_no
end
set @a=@a+1
end

看你的程序,两种结果:不循环和死循环。因为@score没有变化。其实更新个成绩不需要搞的这么复杂,如果想复杂可