===========================创建无参无返回值的存储过程===========================
create proc pro_name
as
--要执行的sql语句--
select * from tablename
--执行存储过程--
exec pro_name
===========================创建有参无返回值的存储过程===========================
--带参存储过程
create proc proc_find_stu(@startId int, @endId int)
as
select * from student where id between @startId and @endId
--执行存储过程
exec proc_find_stu 2, 4;
===========================有参有返回值的存储过程===========================
create proc findUserNameByID
(
@uID int, --sql语句的条件参数
@uName varchar(100) output --带output结尾的是要作为返回值的参数
)
as
select @uName=USER_NAME from dbo.User_Info where User_ID = @uID
--执行
begin
declare @name varchar(100)
-- 此处假设是已知的id号
exec findUserNameByID 3654,@name output
select @name
end
--执行完成后输出对应的用户姓名
===========================分页存储过程================================
-----------创建分页存储过程--------------
create proc selectDivicebyPage
(
@pageIndex int,--页码
@pageSize int--单页记录条数
)
as
declare
--定义每页的起始行数和终止行数
@startRow int=(@pageIndex-1)*@pageSize+1,
@endRow int = (@pageIndex-1)*@pageSize + @pageSize
--或者如下定义
--declare @startRow int, @endRow int
-- set @startRow = (@pageIndex - 1) * @pageSize +1
-- set @endRow = @startRow + @pageSize -1
--此处一次罗列出必须的列,不需要的列不罗列出来,从而提高查询的执行速率
select User_ID,User_Name,User_Password,User_Email
from (select ROW_NUMBER() over(order by User_ID asc) as rownumber,* from dbo.User_Info) a
where a.rownumber between @startRow and @endRow
---------执行分页存储过程------------
--页码为3,单页10条记录
exec selectDivicebyPage 3,10
===========================修改存储过程===========================
alter proc proc_get_student
as
select * from student;
===========================删除存储过程===========================
DROP PROCEDURE Proc_name