黑马程序员之存储过程学习笔记:存储过程的基本使用

来源:互联网 发布:网络诈骗投诉电话多少 编辑:程序博客网 时间:2024/09/21 09:02
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------

存储过程的概念:
存储过程是一种数据库对象,为了实现某个特定的任务,将一组预编译的SQL语句将以一个存储单元的形式存储在服务器上,供用户调用,存储过程在第一次执行时进行编译,然后将编译好的代码保存在高速缓存中,以便以后调用,这样可以提高代码的执行效率。

--创建一个简单的存储过程
create proc  UserId  @name varchar30
as
select LoginId  from  UserInfo  where  LoginPWD=@name 

---执行存储过程
execute  UserId

---查看存储过程的一般信息
exec  sp_help UserId

---查看存储过程的定义信息
exec sp_helptext UserId

---查看存储过程的相关性
exec sp_depends UserId

---创建带有返回参数的存储过程
create  proc score @name float  output
as
select @name=AVG(StudentResult)   from  Result

----执行返回参数的操作
declare @pj  float
exec score  @pj output
print '所有学生的平均成绩是:'+STR(@pj)

---创建多个参数的存储过程
create proc score @studentno  varchar(30) ,@grade float output
as
select @grade=StudentResult  from  Result  where  StudentNo=@studentno
declare @chengji float
exec score '001',@chengji output
print'此学生的成绩是:'+STR(@chengji)
存储过程的优点:
执行速度更快;允许模块化程序设计;提高系统安全性;减少网络流通量。

--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------