数据库开发(存储过程)

1、创建存储过程。
(1)创建无参数的存储过程。

--创建无参数的存储过程
create proc up_stu
as 
select  * from  student where sex=1
--执行存储过程
exec up_stu

在这里插入图片描述
(2)创建有参数的存储过程。

--创建有参数的存储过程
create proc up_stu2  @sno  char(7)
as
if exists (select  * from  student where sno=@sno)
select  student.sno,sname,cno,score from student,stu_course where student.sno=stu_course.sno and student.sno=@sno
else 
print '你提供的学号不存在'
--执行存储过程
exec  up_stu2 @sno='2016003'

在这里插入图片描述
(3)创建多个参数的存储过程。

--创建多个参数的存储过程 注:xh表示学号,kch表示课程号,cj表示成绩
create  proc up_stu3 @xh  char(7),@kch char(4),@cj float
as
update stu_course set score=@cj where sno=@xh and cno=@kch
--执行存储过程
exec up_stu3  @xh='2016006',@kch='005',@cj=100

(4)带有输出参数的存储过程。

--创建带有输出参数的存储过程
create proc stu_out @cno char(4),@avg float output
as
select @avg=avg(score) from stu_course where cno=@cno
--执行存储过程
declare @avg float
exec stu_out '001',@avg out
select @avg as 平均数

在这里插入图片描述