/*select *from oa.student LIMIT 0,2 limit /*能是常量*/
/*(当前的页数-1)*pagesize,pagesize*/
create PROCEDURE SP_Pager(in PageIndex int,in pageSize int,in strWhere varchar(2000),out rowCount int )
BEGIN
DECLARE _strSql varchar(3000) DEFAULT('select *from oa.student ');/*注意空格不要加@符号;注意分号*/
DECLARE _strCount varchar(3000);
set _strCount='select count(1) from oa.student ';
if(PageIndex<=0)
THEN
set PageIndex=1;
end if;
/*判断where是否为空*/
/*判断的注意事项:if ()then end if; 如果有else 没有then 包含在if end if 里面*/
if(strWhere<>'')
then
/*limit 只能是一个常量,*/
set _strSql=CONCAT(_strSql,strWhere,' limit ',(PageIndex-1)*pageSize ,',',pageSize);/*select *from oa.student where 1=1 limit 1,1*/
set _strCount=CONCAT(_strCount,strWhere,' into @TotalCount');
/*(当前的页数-1)*pagesize,pagesize*/
create PROCEDURE SP_Pager(in PageIndex int,in pageSize int,in strWhere varchar(2000),out rowCount int )
BEGIN
DECLARE _strSql varchar(3000) DEFAULT('select *from oa.student ');/*注意空格不要加@符号;注意分号*/
DECLARE _strCount varchar(3000);
set _strCount='select count(1) from oa.student ';
if(PageIndex<=0)
THEN
set PageIndex=1;
end if;
/*判断where是否为空*/
/*判断的注意事项:if ()then end if; 如果有else 没有then 包含在if end if 里面*/
if(strWhere<>'')
then
/*limit 只能是一个常量,*/
set _strSql=CONCAT(_strSql,strWhere,' limit ',(PageIndex-1)*pageSize ,',',pageSize);/*select *from oa.student where 1=1 limit 1,1*/
set _strCount=CONCAT(_strCount,strWhere,' into @TotalCount');
ELSE
set _strSql=CONCAT(_strSql,' limit ',(PageIndex-1)*pageSize ,',',pageSize);/*select *from oa.student where 1=1 limit 1,1*/
set _strCount=CONCAT(_strCount,' into @TotalCount');
end if;
/*执行我们的字符串sql 需要预先编译,将编译内容放到一个变量中,pre_sql 执行预编译的变量, 删除编译*/
set @_sql=_strSql;
PREPARE pre_sql from @_sql;/*预编译存储到pre_sql*/
EXECUTE pre_sql;
/*消亡预编译*/
DEALLOCATE PREPARE pre_sql;
set _strSql=CONCAT(_strSql,' limit ',(PageIndex-1)*pageSize ,',',pageSize);/*select *from oa.student where 1=1 limit 1,1*/
set _strCount=CONCAT(_strCount,' into @TotalCount');
end if;
/*执行我们的字符串sql 需要预先编译,将编译内容放到一个变量中,pre_sql 执行预编译的变量, 删除编译*/
set @_sql=_strSql;
PREPARE pre_sql from @_sql;/*预编译存储到pre_sql*/
EXECUTE pre_sql;
/*消亡预编译*/
DEALLOCATE PREPARE pre_sql;
/*执行count*/
set @_sqlCount=_strCount;
PREPARE pre_Count from @_sqlCount;
EXECUTE pre_Count;
/*获取输出的值必须在消亡之前执行之后*/
set rowCount=@TotalCount;
DEALLOCATE PREPARE pre_Count;
set @_sqlCount=_strCount;
PREPARE pre_Count from @_sqlCount;
EXECUTE pre_Count;
/*获取输出的值必须在消亡之前执行之后*/
set rowCount=@TotalCount;
DEALLOCATE PREPARE pre_Count;
END
set @count=0;
/*调用时 limit不能是负数*/
call SP_Pager(0,10,' where 1=1 ',@count);
select @count;
/*调用时 limit不能是负数*/
call SP_Pager(0,10,' where 1=1 ',@count);
select @count;