【数据库】数据库视频总结(八)

举报
Laura_张 发表于 2022/08/27 23:08:55 2022/08/27
【摘要】                                  第九章 存储过程 目录           &n...

                                 第九章 存储过程

目录

                                                       全局导图

                                                      理论

定义

优点

种类

规则

                                                       

                                                      操作

创建存储过程

更新存储过程

修改存储过程

删除存储过程

 

                                                            其他

临时存储过程

嵌套存储过程

查看存储过程

                                                     感谢您的阅读~


                                      全局导图

 


                                           

                                                      理论

  • 定义

存储过程是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中。存储过程包含程序流、逻辑以及对数据库的查询。可以接受参数、输出参数、返回单个或者多个结果集以及返回值。

  • 优点

1、存储过程与其他应用程序共享应用程序逻辑器,确保了数据访问和修改的一致性。存储过程有封装业务功能。

2、具有安全性和所有权连接,可附加到他们的证书。用户可以被授予权限来执行存储过程而不必直接对存储过程中引用的对象具有权限。

3、存储过程提供了安全机制。没有访问存储过程引用的表或者试图的权限的用户,也可以被授予执行该存储过程的权限。

4、存储过程允许模块化程序设计,存储过程一旦创建,以后可以在程序中调用多次。可以改进应用程序的可维护性。

5、可以减少网络通信流量。

  • 种类

用户自定义存储过程、系统存储过程、扩展存储过程

系统存储过程可以直接修改,也可以通过新建查询来查询系统存储过程。

--示例,常用的几个系统存储过程
exec sp_who sa 
exec sp_helpdb student
exec sp_monitor
  • 规则

                                                       

                                                      操作

创建存储过程

 

实例:

--创建存储过程
create procedure proc_student
@sex varchar(10)
as 
select * from student_Info where student_Sex =@sex 

--调用存储过程
exec proc_student @sex='女' 

 

使用带有默认值的参数

create proc p_emp 
 @tele  varchar(10),
 @Result varchar(20)='成绩'--创建两个参数
 as 

	select A.student_ID ,A.born_Date,A.student_Name,B.result,B.course_Name
	from student_Info A, result_Info  B
	where  A.student_ID=B.student_ID and A.tele_Number =@tele and B.result =@Result
	
--调用
exec P_emp @tele='156',@Result='115'


 

使用输出参数output

使用output关键字来指定我们输出的参数,可以直接加具体的数值。

--创建存储函数
create proc p_sorce
@name varchar(10),
@sorce int output
as 
 select @sorce=avg(result)
 from result_Info a,student_Info b
 where a.student_ID=b.student_ID and b.student_Name=@name 

--调用查看
declare @sorce1 int 
exec p_sorce @name ='张',@sorce=@sorce1 output
print @sorce1 



--或者是直接加数值
declare @sorce1 int 
exec p_sorce'张',@sorce1 output
print @sorce1 

 

更新存储过程

create proc sorceforupdate 
@id varchar(10),
@sorce int 
as 

	update result_Info set result=@sorce  where student_ID=@id

--调用
exec sorceforupdate @id='3',@sorce=100

 

修改存储过程

 

可编程环境-找到存储过程,点击修改,进行简单编辑即可。

--示例编辑结果
USE [Student]
GO
/****** Object:  StoredProcedure [dbo].[proc_student]    Script Date: 2019/11/29 16:57:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[proc_student]
@id varchar(10)
as 
select * from student_Info where student_ID=@id

 

删除存储过程

drop proc proc_student 

 

                                              其他

临时存储过程

# 局部的临时存储过程

## 全局的临时存储过程

实例:

--创建一个简单的临时存储过程
create procedure #get_info
as 
	select a.result,b.student_Name
	from student_Info b,result_Info a
	where a.student_ID=b.student_ID
	order by b.student_Name

exec #get_info --调用临时存储过程

若是新建一个查询,进行上面的调用,就会显示找不到存储过程,因为创建的局部临时存储过程。

下面需要删除局部临时存储过程,然后建立全局临时存储过程。

--删除局部临时存储过程
drop proc #get_info
--创建全局临时存储过程
create procedure ##get_info
as 
	select a.result,b.student_Name
	from student_Info b,result_Info a
	where a.student_ID=b.student_ID
	order by b.student_Name

--然后在不同的连接中进行调用查看,也是可以完成的
exec ##get_info

 

嵌套存储过程

避免重复性工作的出现,最多可以嵌套32层。

create proc get_classinfo
as 
select * from class_Info
exec proc_student1 '男'
print @@nestlevel

exec get_classinf

 

查看存储过程

1、 可编程性-编写存储过程脚本-create到-新查询编辑器窗口(如果要保存成文件的话,最后选择文件。)

2、使用系统存储过程

--使用语句
exec sp_helptext  proc_student

可以查看proc_student

可以直接保存成一个文件。

 

                                             感谢您的阅读~

文章来源: blog.csdn.net,作者:张艳伟_Laura,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Laura__zhang/article/details/103096378

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。