MySql通用存储过程分页代码

create PROCEDURE sp_paging(in _sql varchar(4000),in _curIndex int,in _pageSize int,out _recordCount int)
BEGIN 

 if _pageSize<=1 then 
  set _pageSize=10;
 end if;

 if _curIndex < 1 then 
  set _curIndex = 1; 
 end if;

 set @strsqlcount=CONCAT('select count(*) into @tempCount from (',_sql,') a');
 prepare stmtsqlcount from @strsqlcount; 
 execute stmtsqlcount ; 
 deallocate prepare stmtsqlcount; 
 set _recordCount = @tempCount;

 set _sql=CONCAT('select * from (',_sql,') a ');
 set @strsql = concat(_sql,' limit ',(_curIndex-1)*_pageSize,',',_pageSize); 
 prepare stmtsql from @strsql; 
 execute stmtsql; 
 deallocate prepare stmtsql;

END

 

你可能感兴趣的:(MySql数据库)