教学:数据库存储过程资料_数据库之存储过程
教学:数据库存储过程资料由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“数据库之存储过程”。
教学三:存储过程
一、教学目的(1)掌握T-SQL流控制语句;(2)掌握创建存储过程的方法;(3)掌握存储过程的执行方法;(4)掌握存储过程的管理和维护。
二、教学内容
1、创建简单存储过程
(1)创建一个名为stu_pr的存储过程,该存储过程能查询出051班学生的所有资料,包括学生的基本信息、学生的选课信息(含未选课同学的信息)。要求在创建存储过程前请判断该存储过程是否已创建,若已创建则先删除,并给出“已删除!”信息,否则就给出“不存在,可创建!”的信息。
if exists(select name from sysobjects where name='stu_pr'and type='p')begin print '已删除!' drop procedure stu_pr end else print '不存在,可创建!' go create procedure stu_pr as select * from Student_20103322 left outer join SC_20103322
on(Student_20103322.Sno=SC_20103322.Sno)left outer join Course_20103322 on(Course_20103322.Cno=SC_20103322.Cno)where clano='051' 1
exec stu_pr2、创建带参数的存储过程
(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,Student_20103322.Sno,Sname,DATEDIFF(YEAR,Birth,GETDATE())age,Cname,Grade from Student_20103322,SC_20103322,Course_20103322 where Student_20103322.Sno=SC_20103322.Sno and Course_20103322.Cno=SC_20103322.Cno and Sdept like @Sdept and Sname like @Sname
execute stu_proc1 '计算机系','林红' 3
execute stu_proc1 '信息安全','胡光璟'
(2)创建一个名为Student_sc的存储过程,可查询出某段学号的同学的学号、姓名、总成绩。(学号起始号与终止号在调用时输入,可设默认值)。执行该存储过程。if exists(select name from sysobjects where name='Student_sc'and type='p')begin print '已删除!' drop procedure student_sc end else print '不存在,可创建!' go create procedure Student_sc @Sno1 char(8),@Sno2 char(8)as select Student_20103322.Sno,Sname,SUM(Grade)总成绩 from Student_20103322,SC_20103322,Course_20103322 where Student_20103322.Sno=SC_20103322.Sno and Course_20103322.Cno=SC_20103322.Cno and Student_20103322.Sno>=@Sno1 and Student_20103322.Sno
execute Student_sc '20110000','20110003' 53、创建带输出参数的存储过程
(1)创建一个名为Course_sum的存储过程,可查询某门课程考试的总成绩。总成绩可以输出,以便进一步调用。
if exists(select name from sysobjects where name='Course_sum'and 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_20103322,Course_20103322 where Course_20103322.Cno=SC_20103322.Cno and Cname=@Cname
group by SC_20103322.Cno,Cname
declare @ping int exec Course_sum '高数',@ping output print '高数的考试总成绩为:'+cast(@ping as varchar(20))
(2)创建一执行该存储过程的批处理,要求当总成绩小于100时,显示信息为:“XX课程的总成绩为:XX,其总分未达100分”。超过100时,显示信息为:“XX课程的总成绩为:XX”。
declare @sum int
declare @Cname varchar(20)Exec Course_sum @cname,@sum out begin
if @sum
else
print cast(@cname as varchar)+'课程的总成绩为:'+cast(@sum as varchar)end
declare @sum int
declare @Cname varchar(20)set @Cname='高数' Exec Course_sum @cname,@sum out begin
if @sum
else
print cast(@cname as varchar)+'课程的总成绩为:'+cast(@sum as varchar)end
4、创建带重编译及加密选项的存储过程
创建一个名为update_sc、并带重编译及加密选项的存储过程,可更新指定学号、指定课程号的学生的课程成绩。(学号、课程号由调用时输入)
if exists(select name from sysobjects where name='update_sc'and type='p')begin print '已删除!' drop procedure update_sc end else print '不存在,可创建!' go create procedure update_sc
@sno char(8),@cno char(3),@grade tinyint with RECOMPILE , ENCRYPTION as update SC_20103322 set Grade=@grade
where Sno=@sno and Cno=@cno
declare @sno char(8),@cno char(3),@grade tinyint set @sno='20103322'set @cno='003' set @grade='100' exec update_sc @sno,@cno,@grade begin print cast(@sno as varchar)+'的'+cast(@cno as varchar)+'课程成绩为:'+cast(@grade as varchar)end5、使用T-SQL语句管理和维护存储过程
(1)使用sp_helptext查看存储过程Student_sc的定义脚本
exec sp_helptext student_sc
(2)使用select语句查看Student_sc存储过程的定义脚本(提示:通过查询表sysobjects和表syscomments)
select *
from sysobjects,syscomments where name = 'Student_sc'
(3)将存储过程stu_pr改为查询学号为2011001的学生的详细资料。
alter procedure stu_pr as select * from Student_20103322 left outer join SC_20103322 on(Student_20103322.Sno=SC_20103322.Sno)left outer join Course_20103322 on(Course_20103322.Cno=SC_20103322.Cno)where Student_20103322.Sno='2011001'
(4)删除存储过程stu_pr。
drop procedure stu_pr6、使用SQL Server Management Studio管理存储过程
(1)在SQL Server Management Studio中重新创建刚删除的存储过程stu_pr create procedure stu_pr as begin select * from Student_20103322 left outer join SC_20103322 on(Student_20103322.Sno=SC_20103322.Sno)left outer join Course_20103322 on(Course_20103322.Cno=SC_20103322.Cno)where Student_20103322.Sno='2011001' end
(2)查看存储过程stu_pr,并将该过程修改为查询051班女生的所有资料。
ALTER procedure [dbo].[stu_pr] as begin select * from Student_20103322 left outer join SC_20103322 on(Student_20103322.Sno=SC_20103322.Sno)left outer join Course_20103322 on(Course_20103322.Cno=SC_20103322.Cno)where Student_20103322.Sno='2011001' and Sex='女' end
(3)删除存储过程stu_pr
【完】