|
对学生-课程数据库编写存储过程,完成下列功能; 1. 统计“离散数学”课程的成绩分布情况,即按照各分数段来统计人数; |
create procedurecount_dispersemath
as
begin
declare @beyoung90 int; //定义大于90分的人数
declare @beyoung80 int; //定义大于80分的人数
declare @beyoung70 int; //定义大于70分的人数
declare @beyoung60 int; //定义大于60分的人数
declare @below60 int; //定义小于60分的人数
declare @grade int; //读取游标中的成绩
declare disperse_Cursor cursor for // 定义游标
select grade from sc where cno in(select cno fromcoursewhere cname='离散数学')
open disperse_cursor; //打开游标
set @beyoung90=0;
set @beyoung80=0;
set @beyoung70=0;
set @beyoung60=0;
set @below60=0;
fetch nextfrom disperse_Cursor into@grade;//遍历游标
while @@FETCH_STATUS=0
begin
if@grade>=90
set@beyoung90=@beyoung90+1;
else if @grade>=80
set@beyoung80=@beyoung80+1;
else if @grade>=70
set@beyoung70=@beyoung70+1;
else if @grade>=60
set@beyoung60=@beyoung60+1;
else if @grade>0
set@below60=@below60+1;
fetch next from disperse_Cursor into @grade;
end
close disperse_cursor;//关闭游标
deallocate disperse_cursor;//释放游标
print '成绩在—之间的有:'+space(6)+convert(char(7),@beyoung90);
print '成绩在—之间的有:'+space(6)+convert(char(7),@beyoung80);
print '成绩在—之间的有:'+space(6)+convert(char(7),@beyoung70);
print '成绩在—之间的有:'+space(6)+convert(char(7),@beyoung60);
print '成绩在分以下的有:'+space(6)+convert(char(7),@below60);
|
end;
2.统计任一门课程的平均成绩; |
create procedure gradeavg //读取某一门课程的平均成绩
@cno char(2)=null
as
begin
selectcname,avg(grade)as平均成绩 from sc,coursewhere sc.cno=course.cnoand sc.cno=@cnogroup by cname;
end
|
execute gradeavg '4' 统计所有课程的平均成绩: |
create procedure avggrade //统计所有的课程的平均成绩。
as
begin
selectcname,avg(grade)as平均成绩 from sc,coursewhere sc.cno=course.cnogroup by cname;
end
|
execute avggrade 3.将学生选课成绩从百分制改为等级制(即A、B、C、D、E)。 |
create procedure chargemark
as
begin
selectsname,cname,
caseconvert(int,grade/10) //学生选课成绩改为等级制
when9 then 'A'
when8 then 'B'
when 7 then 'c'
when6 then 'D'
else'E'
end as '总评'
from student,sc,coursewhere student.sno=sc.snoand sc.cno=course.cno;
|
end execute chargemark |
本文介绍了如何使用SQL存储过程来统计离散数学课程中不同分数段的学生人数,包括90分以上、80-90分、70-80分、60-70分及60分以下。此外,还展示了创建计算单门课程平均成绩、所有课程平均成绩的存储过程,以及将学生成绩转换为等级制的存储过程。
2146

被折叠的 条评论
为什么被折叠?



