- 115
- 0
- 约4.44千字
- 约 12页
- 2017-06-20 发布于湖北
- 举报
PAGE
PAGE 12
实验七 存储过程
一、实验目的
(1)掌握T-SQL流控制语句。
(2)掌握创建存储过程的方法。
(3)掌握存储过程的执行方法。
(4)掌握存储过程的管理和维护。
二、实验内容
1、创建简单存储过程
(1)创建一个名为stu_pr的存储过程,该存储过程能查询出051班学生的所有资料,包括学生的基本信息、学生的选课信息(含未选课同学的信息)。要求在创建存储过程前请判断该存储过程是否已创建,若已创建则先删除,并给出“已删除!”信息,否则就给出“不存在,可创建!”的信息。
if exists (select name from sysobjects where name=stu_prand type=p)
begin
print 已删除!
drop procedure stu_pr
end
else
print 不存在,可创建!
go
create procedure stu_proc
as
select *
from S left outer join SC
on (S.Sno=SC.Sno) left outer join C
on (C.Cno=SC.Cno)
where Sdept=CS
exec stu_proc
2、创建带参数的存储过程
(1)创建一个名为stu_proc1的存储过程,查询某系、某姓名的学生的学号、姓名、年龄,选修课程名、成绩。系名和姓名在调用该存储过程时输入,其默认值分别为“%”与“林%”。执行该存储过程,用多种参数加以测试。
if exists (select name from sysobjects where name=stu_proc1 and type=p)
begin
print 已删除!
drop procedure stu_proc1
end
else
print 不存在,可创建!
go
create procedure stu_proc1
@Sdept char(8)=%,@Sname varchar(8)=李%
as
select Sdept,S.Sno,Sname,Sage,Cname,Grade
from S,SC,C
where S.Sno=SC.Sno
and C.Cno=SC.Cno
and Sdept like @Sdept
and Sname like @Sname
执行所创建存储过程:
use student
execute stu_proc1 CS,李勇
execute stu_proc1 信息安全,胡光璟
(2)创建一个名为studentStudent_sc的存储过程,可查询出某段学号的同学的学号、姓名、总成绩。(学号起始号与终止号在调用时输入,可设默认值)。执行该存储过程。
if exists (select name from sysobjects where name=Student_scand type=p)
begin
print 已删除!
drop procedure student_sc
end
else
print 不存在,可创建!
go
create procedure Student_sc
@Sno1 char(8),@Sno2 char(8)
as
select S.Sno,Sname,SUM(Grade)总成绩
from S,SC,C
where S.Sno=SC.Sno
and C.Cno=SC.Cno
and S.Sno=@Sno1
and S.Sno=@Sno2
group by S.Sno,Sname
执行所创建存储过程:
execute Student_sc 95000,95009
3、创建带输出参数的存储过程
(1)创建一个名为courseCourse_sum的存储过程,可查询某门课程考试的总成绩。
总成绩可以输出,以便进一步调用。
if exists (select name from sysobjects where name=Course_sumand type=p)
begin
print 已删除!
drop procedure Course_sum
end
else
print 不存在,可创建!
go
create procedure Course_sum
@Cname varchar(20),@sum int output
as
select @sum=sum(Grade)
from SC,C
where C.Cno=SC.Cno
and Cname=@Cname
group by SC.Cno,Cname
d
原创力文档

文档评论(0)