SQLSever--存储过程

来源:互联网 发布:人工智能高清免费视频 编辑:程序博客网 时间:2024/05/22 14:53

只是自己的学习记录

存储过程定义:

是一组为了完成特定功能的SQL语句的集合。

优点:

简单--把处理封装在一个易用的单元,可以简化复杂操作;

安全--数据的一致性,如有改动,只需更改存储过程中的代码;

高性能--存储过程是预编译,比批处理更快。

存储过程的种类

1.系统存储过程:以sp_开头,用来进行系统的各项设定.取得信息.相关管理工作,如 sp_help就是取得指定对象的相关信息

2.扩展存储过程 以XP_开头,用来调用操作系统提供的功能

3.用户自定义的存储过程,这是我们所指的存储过

数据库例子:

创建存储过程:

1.创建带一个参数的存储过程

use test;  create proc getcomment  (@VendId varchar(15))  as   select * from [test].[dbo].[Products] where vend_id = @VendId  go

But运行结果是

解决方法:

use test;  go  create proc getcomment  (@VendId varchar(15))  as   select * from [test].[dbo].[Products] where vend_id = @VendId  go

注:go表示一个批处理的结束,局部变量限制在一个批处理范围内,不可以在go后面引用在go之前定义的局部变量。

执行存储过程

use test;  exec getcomment ve01 //传入参数

结果:

2.创建有输入输出参数的存储过程

use test;  go  create proc getCount@VendId varchar(15),@count int outputas select @count = COUNT(*) from  [test].[dbo].[Products] where vend_id = @VendId  go

执行存储过程

declare @sum int   exec getCount ve01,@sum output  select @sum as count

结果: