不需要使用游标循环来读取,直接select * into @变量就OK了,还可以传递参数来执行。
1
MySql通用分页存储过程
2![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
3
过程参数
4
p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4000),p_order varchar(100),p_pageindex int,p_pagesize int,out p_recordcount int,out p_pagecount int
5![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
6
$$:begin
7
declare v_sqlcounts varchar(4000);
8
declare v_sqlselect varchar(4000);
9
#拼接查询总记录的SQL语句
10
set v_sqlcounts = concat('select count(*) into @recordcount from ',p_tables,p_where);
11
#select v_sqlcounts;leave $$;
12
set @sqlcounts = v_sqlcounts;
13
prepare stmt from @sqlcounts;
14
execute stmt;
15
deallocate prepare stmt;
16
#获取动态SQL语句返回值
17
set p_recordcount = @recordcount;
18
#根据总记录跳数计算出总页数
19
set p_pagecount = ceiling((p_recordcount+0.0)/p_pagesize);
20
if p_pageindex <1 then
21
set p_pageindex = 1;
22
elseif p_pageindex > p_pagecount and p_pagecount <> 0 then
23
set p_pageindex = p_pagecount;
24
end if;
25
#拼接分页查询记录的动态SQL语句
26
set v_sqlselect = concat('select ',p_cloumns,' from ',p_tables,p_where,if(p_order is not null,p_order,''),' limit ',(p_pageindex-1)*p_pagesize,' , ',p_pagesize);
27
#select v_sqlselect;leave $$;
28
set @sqlselect = v_sqlselect;
29
prepare stmtselect from @sqlselect;
30
execute stmtselect;
31
deallocate prepare stmtselect;
32
end $$
1
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
2
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
3
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
4
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
5
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
6
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
7
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
8
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
9
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
10
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
11
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
12
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
13
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
14
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
15
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
16
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
17
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
18
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
19
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
20
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
21
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
22
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
23
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
24
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
25
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
26
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
27
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
28
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
29
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
30
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
31
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)
32
![/](http://edu.codepub.com/uploadfile/2010/1229/20101229044714606.gif)