--第一种 declare@avgscoreint set@avgscore= (selectavg(score) from score where studentid = (select studentid from student where name ='白燕') ) --第二种 select@avgscore= (selectavg(score) from score where studentid = (select studentid from student where name ='白燕') ) set@avgscore=50
--select count(studentid) from score where score < 60 declare@countint set@count= (selectcount(studentid) from score where score <60) select*from score while(@count>0) begin update score set score = score +1where score <100 select@count=count(studentid) from score where score <60 end
while((selectcount(studentid) from score where score <60) >0) begin update score set score = score +1where score <100 end
--select * from student
select name "姓名", avg(score) "平均分", "评价" = case when(avg(score) >90) then'优秀' when(avg(score) >80) then'良好' when(avg(score) >70) then'中等' when(avg(score) >60) then'及格' else'不及格' end from score join student on score.studentid = student.studentid groupby name
createfunction comment_avgScore(@name nvarchar(50)) returnsvarchar(20) as begin declare@commentvarchar(20) select@comment= case when(avg(score) >90) then'优秀' when(avg(score) >80) then'良好' when(avg(score) >70) then'中等' when(avg(score) >60) then'及格' else'不及格' end from score join student on score.studentid = student.studentid where name =@name return@comment end
--查询学号为G11310000000002的学生的姓名和年龄,并进一步查询比他出生年份晚一年的学生的学号和年龄。 declare@year nvarchar(4), @birthday datetime select name, 2021-datepart(YYYY,birthday) from student where studentid ='G11310000000002' set@birthday= ( select birthday from student where studentid ='G11310000000002' ) set@year= datepart(YYYY, @birthday) select studentid, birthday from student where datepart(YY, birthday) =@year+1 GO
8_4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
--上机练习8_4 --查询课程编号为2的课程最后一次考试是否有不及格的学生,如果有,所有学生成绩加2分, --直到所有学生都及格。因为成绩列有检查约束,成绩在0到100之间,所以要求成绩达到99分的同学不加分。 use StudentInfo declare@numint, @testTime datetime; select@testtime=max(testtime) from score where subjectid =2 while(1>0) begin set@num= (selectcount(studentid) from score where score <60and testtime =@testtime) if(@num>0) begin update score set score = score +2 where score <=98and subjectid =2and testtime =@testtime end else break; end
--上机练习8_5 --查询参加最近一次课程编号为3的课程考试的学生姓名、成绩和成绩等级,按成绩降序排列。 --如果成绩不低于90分,成绩等级为“优秀”,如果成绩在不低于80分且小于90分,成绩等级为“良好”, --如果成绩不低于70分且小于80分,成绩等级为“中等”, --如果成绩不低于60分且小于70分,成绩等级为“及格”,如果成绩低于60分,成绩等级为“不及格”。 use StudentInfo declare@lasttime datetime; set@lasttime= (selectmax(testtime) from score where subjectid =3) select name, score, 等级 = case when score >=90then'优秀' when score >=80and score <90then'良好' when score >=70and score <80then'中等' when score >=60and score <70then'及格' else'不及格' end from score s innerjoin student sd on s.studentid = sd.studentid where testtime =@lasttimeand subjectid =3 orderby score desc
--上机练习8-7 --1、定义一个求最近一次考试的某门课程的平均成绩的函数, --要求参数是课程号。函数名称为getavgScore_bySubjectID。如图8-17所示。 --2、定义一个多语句表值函数返回参加某门课程考试的学生的姓名、课程名和成绩,并按成绩升序排序。 --要求参数是课程名称。(这样以后查询学生考试结果只需调用函数就可以了)。 createfunction getavgScore_bySubjectID(@subjectidint) returnsnumeric as begin declare@testtime datetime; declare@avg_score numeric; set@testtime= (selectmax(testtime) from score where subjectid =@subjectid) set@avg_score = (selectavg(score) from score where subjectid =@subjectidand testtime =@testtime) return@avg_score end