关于存储过程的一些小知识

存储过程

什么是存储过程

存储过程是一个预编译的SQL语句,优点是允许模块化的设计,就是说只需创建一次,以后在程序中就可以调用多次。如果某次操作需要执行多次SQL,使用存储过程比单纯SQL语句执行要快。可以用一个“execute 存储过程名 参数”命令来调用存储过程。

存储过程及其优缺点。

存储过程是一个预编译的sql语句 ,编译后可多次使用
优势:响应时间上来说有优势,可以给我们带来运行效率提高的好处,且使用存储过程的系统更加稳定
缺点:维护性较差,相对于简单sql,存储过程并没有什么优势,并且在进行调试时比较困难

存储过程的创建以及使用

不带存储过程
select * from Books

创建存储过程
go
create proc cp_select_book
as
select * from Books

调用存储过程
exec cp_select_book

带参数存储过程
go
alter proc cp_select_book_byName
(
@name nvarchar(50)
)
as
select * from Books where Name like @name
调用存储过程
exec cp_select_book_byName ‘js’

带输入,输入参数
进行模糊查询,返回记录数,还要返回记录
create proc cp_select_book_byNameEx
(
@name nvarchar(50),
@rowCount int output
)
as
select * from Books where Name like ‘%’+@name+’%’
select @rowCount=COUNT(*) from Books where Name like ‘%’+@name+’%’

调用
go
定义一个变量,来接受返回值@rowCount
declare @rows int
exec cp_select_book_byNameEx ‘js’,@rows out
print @rows

go
create proc cp_select_name
(
@name nvarchar(20),
@pageindex int,
@pagecount int
)
as

select * from Book where name like ‘%’+@name+’%’ order by ID offset @pagecount*@pageindex rows fetch next @pagecount rows only

lter proc cp_select_ByNamePage
(
@name nvarchar(50), --名称
@pageindex int, --页码
@pagecount int, --每页显示多少条
@pageRows int out --输出参数满足条件的函数
)
as
select top (@pagecount) * from Book where id not in(select top ((@pageindex-1)*@pagecount) id
from book where name like ‘%’+@name+’%’ order by id )
and name like ‘%’+@name+’%’
order by id

满足条件共多少条
select @pageRows=count(*) from book where name like ‘%’+@name+’%’
go
exec cp_select_ByNamePage ‘建筑’,@pageindex=2,@pagecount=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值