自己做了一個小測試,測試方法如下:
#drop procedure if exists getPageNo
create procedure getPageNo
(
in id varchar(10)
)
begin
declare pageNo int;
declare pageSize int;
declare pageCount int;
declare startIndex int;
declare endIndex int;
declare returnNo int;
set pageSize=10;
set pageNo = 1;
set pageCount=(select ceiling(count(*)/pageSize) from article); #get the record count
set endIndex=0;
set startIndex=(pageNo-1)*pageSize;
set endIndex=pageSize;
set @idCount=0;
loop_label:
while pageNo <= pageCount do
set @sqlcnt=concat('select count(*) into @idCount from(select orgid from article order by center,service limit ',startIndex,',',endIndex,') as tmp where
a_id=/'',id,'/'');
prepare s_cnt from @sqlcnt;
execute s_cnt;
deallocate prepare s_cnt;
if((@idCount)!=0) then
set returnNo=pageNo;
leave loop_label;
end if;
set pageNo = pageNo + 1;
set startIndex = (pageNo-1) * pageSize;
set endIndex = pageSize;
end while;
#show result
select returnNo ;
end;
測試存儲過程:
call getPageNo('4124');
因爲我沒有給存儲過程返回值,所以我沒有把結果存到返回值中。