文档详情

存储过程、触发器和用户自定义函数实验.doc

发布:2017-06-15约5.21千字共10页下载文档
文本预览下载声明
存储过程、触发器和用户自定义函数实验 实验内容一 练习教材中存储过程、触发器和用户自定义函数的例子。教材中的BookSales数据库,在群共享中,文件名为BookSales.bak。 针对附件1教学活动数据库,完成下面的实验内容。 (1,该存储过程统计“高等数学”的成绩分布情况,即按照各分数段统计人数。 CREATE Proc MATH_NUM @MATH CHAR(20)=高等数学 AS SELECT @MATH as canme, count(case when score=90 then 1 end)as[90以上], ?count(case when score=80 and score90 then 1 end)as[80-90], count(case when score=70 and score80 then 1 end)as[70-80], count(case when score=60 and score70 then 1 end)as[60-70], count(case when score60 then 1 end)as[60以下] FROM study,course WHERE study.cno=course.cno and course.cname=@MATH GROUP BY course.cname (2课程号,该存储过程统计给定课程的平均成绩。 CREATE Proc AVG_SCORE @cno CHAR(20) AS SELECT @cno as 课程号,course.cname as 课程名,STR(AVG(score),5,2) as 平均成绩 FROM study,course WHERE study.cno=course.cno and course.cno=@cno GROUP BY course.cname (3A、B、C、D、E)。 CREATE Proc SCORE_CHANGE AS SELECT course.cname as 课程名,study.sno as 学号,study.cno as 课程号,study.score as 成绩, case when score=90 and score=100 thenA when score=80 and score90 thenB when score=70 and score80 thenC when score=60 and score70 thenD when score60 thenE end as 等级 from study,course where study.cno=course.cno (4创建一个存储过程,该存储过程有一个参数用来接收学生姓名,该存储过程查询该学生的学号以及选修课程的门数。 CREATE Proc STUDENT_STUDY @name varchar(20) AS select @name as 姓名,study.sno as 学号,count(cno) as 选修门数 from study,student where study.sno=student.sno and sname=@name group by study.sno (5创建一个存储过程,该存储过程有两个输入参数用来接收学号和课程号,一个输出参数用于获取相应学号和课程号对应的成绩。 (1)为study表创建一个UPDATE触发器,当更新成绩时,要求更新后的成绩不能低于原来的成绩。 CREATE TRIGGER UPDATE_SCORE ON study instead of update as declare @sno2 char(5),@cno2 char(4),@score1 smallint,@score2 smallint select @sno2=sno,@cno2=cno,@score2=score from inserted select @score1=score from deleted if(@score2=@score1) update study set score=@score2 where study.cno=@cno2 and study.sno=@sno2 go (2)为study表创建一个DELETE触发器,要求一次只能从study表中删除一条记录。 CREATE TRIGGER DEL_STUDY ON study instead of DELETE AS begin declare @num int,@sno char(5),@cno char(4) select @num=COUNT(*)from deleted if @num=1 begin select @sno=sno,
显示全部
相似文档