01.存储过程简述
one.基本语法
create procedure 存储过程名称 --可简写用proc
(
@参数名1 数据类型,
@参数名2 数据类型,
......
@参数名n 数据类型 output --output表示该参数用于输出
)
as
begin
执行语句或查询语句等等
end
two.无参数的存储过程
create proc proc_ memberCount
as
begin
declare @memberCount int --declare用于声明变量
set @memberCount= (select COUNT(*) from Student )
print '班级总人数是:' + cast(@memberCount as nvarchar(20))
end
three.执行存储过程和删除存储过程
exec 存储过程名称(参数列表) --这是执行存储过程,execute的简写
【例如】:exec proc_ memberCount
drop proc 存储过程名称 --这是删除存储过程,procedure的简写
【例如】drop proc pro_stuPaging
02.登录存储过程
create proc proc_IsLogin
@userName nvarchar(20),
@userPwd nvarchar(20),
@result int output
as
if exists (select * from Student where StuLoginName=@userName and StuLoginPwd=@userPwd )
begin
set @result=1 --1表示登录成功
end
else
set @result=0 -- 0表示登录失败
go
03.分页存储过程
create procedure pro_stuPaging
(
@pageIndex int, --当前页
@pageSize int,--总页数
@count int output--总记录数
)
as
select top (@pageSize)*
from Student
where StuID not in
(select top (@pageSize*(@pageIndex-1)) StuID from Student)
select @count=count(*) from Student
exec pro_stuPaging 3, 2,4;--执行存储过程
go
【Moment】:如果出现问题的时候,不那么焦躁,而是冷静解决,这大概就是一次不错的自我存储能量的过程。