数据库存储过程

存储过程

创建多种种存储过程

(1)不带参数的存储过程
eg: 使用create procedure 命令建立名为StudentCourseScore存储过程,该存储过程用于查询“学生选课名称、学分及分数”视图中的数据。写出程序代码。

--(1)
use student
go
if exists (select name from sysobjects 
where name='pr_StudentCourseScore' and type = 'P')
drop procedure pr_StudentCourseScore
go
create procedure pr_StudentCourseScore
as
select 课程.课程名称,课程.学分, 学生成绩.分数
from 学生成绩,课程
where 学生成绩.课程号 = 课程.课程号
go
exec pr_StudentCourseScore
go 


(2)带输入参数的存储过程
eg:使用带参数的存储过程StudentAge,根据制定的“年龄”,找出与给定“年龄”相等的学生的“学号”和“姓名”。写出程序代码。

use student
go
if exists (select name from sysobjects where name='pr_StudentAge' and type = 'P' )
drop procedure pr_StudentAge
go
create procedure pr_StudentAge @age int
as
select 学号,姓名
from 学生信息
where 年龄=@age
go
exec pr_StudentAge 19
go

(3)带输出参数的存储过程
eg:在上题中设置@count参数,作为输出参数,返回和给定“年龄”相同的学生的总人数。写出程序代码。

use student
go
if exists (select name from sysobjects where name='pr_StudentAge' and type = 'P' )
drop procedure pr_StudentAge
go
create procedure pr_StudentAge @age int,@count int output
as
select @count = count(*)
from 学生信息
where 年龄=@age
go
declare @Scount int 
exec pr_StudentAge 20,@Scount output --执行时要给实参去接收形参
print '年龄为19的学生总数为:'+cast(@Scount as char(4)) 
--cast函数将int型Scount转化为char型 ,并返回值
go

(4)加密存储过程
(5)嵌套使用存储过程(最多32层)

存储过程 属性查看,修改,删除

(1)查看存储过程信息
exec sp_helptext 存储过程名字
(2)修改存储过程
use 数据库名字
go
alter procedure 存储过程名字

重写一遍存储过程

(3)删除存储过程
drop proceduer 存储过程名字

利用存储过程对数据 插入,修改,删除

(1)插入
eg:使用存储过程实现向“学生信息”表插入一条记录的操作。

use student
go
if exists (select name from sysobjects where name = 'pr_InsStuInfo' and type = 'P')
drop procedure pr_InsStuInfo
go
create procedure pr_InsStuInfo @学号 char(7),@姓名 char(20),@性别 char(2),@年龄 int,
@所在系 char(15),@flag int output
as
begin
		set @flag = 1
		insert into 学生信息 values(@学号,@姓名,@性别,@年龄,@所在系)
		if @@ERROR<>0
		set @flag = 0
end
return
go
declare @flag int
set @flag = 0
exec pr_InsStuInfo '008','张三','男',21,'计算机',@flag output
if @flag = 1
print '学生信息添加成功!'
else
print '学生信息添加失败!'
go

(2)修改
eg:在“学生信息”表中,修改和所给的“学号”相同的记录,用存储过程实现。

use student
go
if exists(select name from sysobjects 
		where name = 'pr_UpdStuInfo' and type = 'P')
drop procedure pr_UpdStuInfo
go
create procedure pr_UpdStuInfo @学号 char(7),@姓名 char(20),@性别 char(2),@年龄 int,
@所在系 char(15),@flag int output
as
set @flag = 1
if exists (select * from 学生信息 where 学号 = @学号)
update 学生信息
set 姓名=@姓名,性别=@性别,年龄=@年龄,所在系=@所在系
where 学号 = @学号
else
set @flag = 0
return
go
declare @flag int
set @flag = 0
exec pr_UpdStuInfo '008','张三','男',21,'计算机',@flag output
if @flag = 1
print '学生信息修改成功!'
else
print '学生信息修改失败!’

go

(3)删除
eg:在“学生信息”表中,删除和所给的“学号”相同的记录,用存储过程实现。

use student
go
if exists (select name from sysobjects
				where name ='pr_DelStuInfo' and type = 'P')
drop procedure pr_DelStuInfo
go
create procedure pr_DelStuInfo @学号 char(7),@flag int output
as
begin
set @flag = 1
if exists (select * from 学生信息 where 学号 = @学号)
delete 学生信息 where 学号=@学号
else
set @flag = 0
end
return
go
declare @flag int,@学号 char(7)
set @学号 = '008'
set @flag = 0
exec pr_DelStuInfo @学号,@flag output
if @flag = 1
print '该学生信息删除成功!'
else
print '该学生不存在,无法删除!'
go


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章