MySQL分页查询存储过程
-- 分页查询
delimiter $
create procedure p_List
(
in i_Name varchar(50),
in i_CName varchar(20),
in pageIndex int,
in pageSize int,
out pageCount int
)
begin
declare startIndex int;
declare t_error INT DEFAULT 0;
declare CONTINUE HANDLER FOR SQLEXCEPTION,SQLWARNING,not found set t_error=1;
start TRANSACTION;
set startIndex=(pageIndex-1)*pageSize;
SET @row_number = 0;
select (@row_number:=@row_number + 1) AS num,a.Id,a.Name,a.Sex,a.Age,a.Hobby,a.CId,b.CName from Students a join Classes b on a.CId=b.CId where a.Name like concat('%',i_Name,'%') and b.CName like concat('%',i_CName,'%') limit startIndex,pageSize;
select count(1) from Students a join Classes b on a.CId=b.CId where a.Name like concat('%',i_Name,'%') and b.CName like concat('%',i_CName,'%') into pageCount;
IF t_error = 1 THEN ROLLBACK;
ELSE COMMIT;
END IF;
end $