--现在假设你的表[stuMarks]所在数据库名字为DB,在[stuMarks]表中你有至少这样两个字段:学生编号stu_id、学生成绩score,你可以运行下面的批处理来实现这个“和谐”的过程。--关于按分数排等级,我也写了个statement,可以根据你的具体情况来修改,你应该能看懂大概意思.use DBdeclare @currentid intdeclare @currentscore 展开
--现在假设你的表[stuMarks]所在数据库名字为DB,在[stuMarks]表中你有至少这样两个字段:学生编号stu_id、学生成绩score,你可以运行下面的批处理来实现这个“和谐”的过程。--关于按分数排等级,我也写了个statement,可以根据你的具体情况来修改,你应该能看懂大概意思.use DBdeclare @currentid intdeclare @currentscore intdeclare @avgscore intselect @avgscore= AVG(score) from [stuMarks]declare mycursor cursor local SCROLL scroll_locks --利用游标遍历整个表,优先给分数最低的人加分 for select stu_id,score from [stuMarks] order by score open mycursor while @avgscore <60 begin fetch next from mycursor into @currentid, @currentscore if @@fetch_status=-1 fetch first from mycursor into @currentid, @currentscore if @currentscore<=98 --学生的分数必须小于或等于98,否则再加分就露馅了。 begin update [stuMarks] set score=score+2 where stu_id=@currentid select @avgscore= AVG(score) from [stuMarks] end end close mycursor--分数分级:select stu_id, (case when score between 90 and 100 then N'优' when score between 80 and 90 then N'良' when score between 60 and 80 then N'中' else N'差' end) as dengji from [stuMarks] 收起