SQLServer实验三教案.docVIP

  • 3
  • 0
  • 约5.08千字
  • 约 11页
  • 2017-09-08 发布于湖北
  • 举报
实验七 (1)创建并运行存储过程student_grade,要求实现如下功能:查询studb数据库中每个学生各门课的成绩,其中包括每个学生的sno、sname、cname和score。 create procedure student_grade as select student.sno,student.sname,course.cname,student_course.score from student join student_course on student.sno=student_course.sno join course on course.cno=student_course.cno 运行结果代码: use Studb go execute student_grade go (2)创建并运行名为proc_exp的存储过程,要求实现如下功能:从student_course表中查询某一学生考试的平均成绩。 create procedure proc_exp @sname varchar(8) as begin select sname,AVG(score) from student join student_course on student.sno=student_course.sno where sname=@sname group by sname end 运行结果代码: use Studb go execute proc_exp @sname=刘招香 go (3)修改存储过程proc_exp,要求实现如下功能:输入学生学号,根据该学生所选课程的平均成绩给出提示信息,即如果平均成绩在60分以上,显示“成绩合格,成绩为XX分”,否则显示“成绩不合格,成绩为XX分”;然后调用存储过程proc_exp,输入学号0705010131,显示成绩是否合格。 alter procedure proc_exp @student_sno varchar (20) as declare @avg varchar(20) set @avg=(select AVG(score) from student_course where sno=@student_sno) if @avg=60 print 成绩合格,成绩为+@avg+分 else print 成绩不合格,成绩为+@avg+分 use Studb go declare @student_sno varchar (20) select @student_sno=0705010131 exec proc_exp @student_sno (4)创建名为proc_add的存储过程,要求实现以下功能:向student_course表中添加学生记录;然后调用存储过程proc_add,向student_course表中添加学生成绩记录。 create procedure proc_add @sno char(10), @cno char(10), @score tinyint as begin set nocount on if not exists (select * from student_course where sno=@sno and cno=@cno and score=@score ) insert into student_course(sno,cno,score) values(@sno,@cno,@score) end 运行结果代码: use studb go exec proc_add 0705010102,0208,80 go 执行前: 执行后: (5)删除存储过程proc_exp和proc_add IF OBJECT_ID(proc_exp) IS NOT NULL DROP PROCEDURE proc_exp IF OBJECT_ID(proc_add) IS NOT NULL DROP PROCEDURE proc_add 实验八 (1)创建触发器student_trg,当删除student表中的数据时,同时删除student_course表中相同的数据;然后通过删除student表中的某个学生记录来验证该触发器。create trigger student_trg on student after delete as begin delete from student_course where sno IN( select sno from deleted ) end 运行结果代码: DELETE FROM student WHERE sno=070501030

文档评论(0)

1亿VIP精品文档

相关文档