实验项目名称: 存储过程和触发器 实验学时: 2 同组学生姓名: 实验地点: 实验日期: 实验成绩: 批改教师: 批改时间:
一、实验目的和要求
1、通过对常用系统存储过程的使用,了解存储过程的类型;
2、通过创建和执行存储过程,了解存储过程的基本概念,掌握使用存储过程的操作技巧和方法;
3、通过对已创建的存储过程的改变,掌握修改、删除存储过程的技巧; 4、了解触发器的基本概念,理解触发器的功能; 5、掌握创建、修改和删除和使用触发器的操作方法。
二、实验设备、环境
设备:奔腾Ⅳ或奔腾Ⅳ以上计算机;
环境:WINDOWS 2000 SERVER或WINDOWS 2003 SERVER、SQL Server 2005中文版。
三、实验步骤
1、根据题目要求熟悉SQL Server 2005的各种管理工具; 2、分析题意,重点分析题目要求并给出解决方法;
3、按题目要求完成实际操作任务,并将相关文档资料保存在以自己学号命名的文件夹中; 4、提交完成的实验结果。
四、实验内容
一、存储过程的类型。
1、使用sp_helptext查看byroyalty存储过程的文本,该存储过程在数据库pubs中。
sp_helptext byroyalty;
二、创建与执行存储过程
1、在MyDB中创建存储过程proc_1,要求实现如下功能:产生学分为4的课程学生选课情况列表,其中包括课程号、课程名、学分、学号、姓名、专业、性别等。并调用此存储过程,显示执行结果。
create or replace PROCEDURE proc_1 as
cno
char(4);cname
varchar(16);credit
int;sno
char(8);sname
varchar(10);specially varchar(3);sex char(2); begin select
course.cno,course.cname,course.credit,student.sno,student.sname,student.sex,class1.specially
into cno,cname, credit,sno,sname,sex, specially from course,student,grade,class1
where course.cno = grade.cno and student.sno = grade.sno and class1.clsno = student.clsno and course.credit = 4; end proc_1;
2、在MyDB中创建存储过程proc_2,要求实现如下功能:输入专业名称,产生该专业学生的选课情况列表,其中包括专业、学号、姓名、课程号、课程名、成绩、学分等。并调用此存储过程,显示“计算机应用”专业学生的选课情况列表。 create or replace
PROCEDURE proc_2(spec in varchar) as
cno
char(4);cname
varchar(16);credit
int;sno
char(8);sname
varchar(10);specially varchar(3);score numeric(4,2); BEGIN
select class1.specially,student.sno,student.sname,course.cno, course.cname,grade.score,course.credit
into specially,sno,sname,cno,cname,score,credit from class1,student,course,grade
where student.sno = grade.sno and course.cno = grade.cno and student.clsno = class1.clsno and class1.specially = spec; end;
3、在MyDB中创建存储过程proc_3,要求实现如下功能:输入学生学号,根据该学生所选课程的总学分显示提示信息,如果总学分<9,则显示“此学生学分不足!”,否则显示“此学生学分已足!”,并调用此存储过程,显示“19920102”学生的总学分情况。
create procedure proc_3(@sno1 char(8)) as declare
@Total tinyint;
select @Total=sum(course.credit) from course,student,grade
where student.sno = grade.sno and course.cno = grade.cno and if @Total < 9
print '此学生学分不足' else begin
student.sno = @sno1
print '此学生学分已足'
end;
exec proc_3 19920106
三、修改存储过程
1、对MyDB中已创建的存储过程proc_1进行修改,要求在显示列表中增加班级字段,即产生学分为“4”的课程学生选课情况列表,其中包括课程号、课程名、学分、学号、姓名、专业、班级、性别等. alter PROCEDURE proc_1
as cno char(4);clsname char(10);cname varchar(16);credit int;sno char(8);sname varchar(10);specially varchar(3);score numeric(4,2); BEGIN select
course.cno,course.cname,course.credit,student.sno,student.sname,student.sex,class1.specially,class1.claname from course,student,grade,class1
where course.cno = grade.cno and student.sno = grade.sno and class1.clsno = student.clsno and course.credit = 4; end;
exec proc_1;
2、在MyDB中创建的存储过程proc_2进行修改,要求实现如下功能:输入专业名称,产生该专业所有男生的选课情况列表,其中包括专业、学号、姓名、课程号、课程名、成绩、学分等。并调用修改后的存储过程,显示“计算机应用”专业男生的选课情况列表。 alter or replace
PROCEDURE proc_2(spec in varchar) as
cno
char(4);cname
varchar(16);credit
int;sno
char(8);sname
varchar(10);specially varchar(3);score numeric(4,2); BEGIN
select class1.specially,student.sno,student.sname,course.cno, course.cname,grade.score,course.credit from class1,student,course,grade
where student.sno = grade.sno and course.cno = grade.cno
and student.clsno = class1.clsno and student.sex = '男' and class1.specially = spec;end;
exec proc_2 ‘计算机应用’;
3、对MyDB中已创建的存储过程proc_3进行修改,要求实现如下功能:输入学生学号,根据该学生所选课程的总学分显示提示信息,如果总学分<9,则显示“此学生所选总学分为XXX,学分不足!”,否则显示“此学生所选总学分为XXX,学分已足!”。并调用修改后的存储过程,显示“19920102”学生的总学分情况。 alter or replace
procedure proc_3(sno1 in char) is
Total int;
begin
select sum(grade.credit) into Total
from course,student,grade
where student.sno = grade.sno and course.cno = grade.cno and student.sno = sno1;