最近给客户做的一小系统是SQLSERVER的数据库,因为特殊原因要切换到MYSQL上去,切换数据库确实让人头疼的,SQLSERVER和MYSQL的存储过程还是有很大差别的,下面是我做切换时转换的MYSQL版的分页过程,和事务处理的一个测试过程,事务处理也不像SQLSERVER那样。不多说了,供学习MYSQL的兄弟们参考下,我用的MYSQL5.5版本,经过测试都是可行的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<span style="color: #008080;">/*</span><span style="color: #008080;"> --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输入参数:@tables -- 要查询的表 --输入参数:@where -- 查询条件 --输入参数:@orderby -- 排序字段 --输出参数:@page -- 当前页计数从1开始 --输出参数:@pagesize -- 每页大小 --输出参数:@totalcount -- 总记录数 --输出参数:@pagecount -- 总页数 </span><span style="color: #008080;">*/</span> <span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">PROCEDURE</span> <span style="color: #0000ff;">IF</span> <span style="color: #808080;">EXISTS</span> Query_Pagination; <span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">PROCEDURE</span> Query_Pagination ( <span style="color: #808080;">in</span> _fields <span style="color: #0000ff;">varchar</span>(<span style="font-weight: bold; color: #800000;">2000</span>), <span style="color: #808080;">in</span> _tables <span style="color: #0000ff;">text</span>, <span style="color: #808080;">in</span> _<span style="color: #0000ff;">where</span> <span style="color: #0000ff;">varchar</span>(<span style="font-weight: bold; color: #800000;">2000</span>), <span style="color: #808080;">in</span> _orderby <span style="color: #0000ff;">varchar</span>(<span style="font-weight: bold; color: #800000;">200</span>), <span style="color: #808080;">in</span> _pageindex <span style="color: #0000ff;">int</span>, <span style="color: #808080;">in</span> _pagesize <span style="color: #0000ff;">int</span>, <span style="color: #808080;">in</span> _sumfields <span style="color: #0000ff;">varchar</span>(<span style="font-weight: bold; color: #800000;">200</span>),<span style="color: #008080;">/*</span><span style="color: #008080;">增加统计字段2013-5-8 peaceli</span><span style="color: #008080;">*/</span> out _totalcount <span style="color: #0000ff;">int</span> , out _pagecount <span style="color: #0000ff;">int</span> ) <span style="color: #0000ff;">begin</span> <span style="color: #0000ff;">set</span> <span style="color: #008000;">@startRow</span> <span style="color: #808080;">=</span> _pageSize<span style="color: #808080;">*</span>(_pageIndex <span style="color: #808080;">-</span><span style="font-weight: bold; color: #800000;">1</span>); <span style="color: #0000ff;">set</span> <span style="color: #008000;">@pageSize</span> <span style="color: #808080;">=</span> _pageSize; <span style="color: #0000ff;">set</span> <span style="color: #008000;">@rowindex</span> <span style="color: #808080;">=</span> <span style="font-weight: bold; color: #800000;">0</span>; <span style="color: #0000ff;">set</span> <span style="color: #008000;">@strsql</span> <span style="color: #808080;">=</span> CONCAT(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">select sql_calc_found_rows @rowindex:=@rowindex+1 as rownumber,</span><span style="color: #ff0000;">'</span>,_fields,<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> from </span><span style="color: #ff0000;">'</span>,_tables,<span style="color: #ff00ff;">case</span> ifnull(_<span style="color: #0000ff;">where</span>,<span style="color: #ff0000;">''</span>) <span style="color: #0000ff;">when</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">then</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">else</span> concat(<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> where </span><span style="color: #ff0000;">'</span>,_<span style="color: #0000ff;">where</span>) <span style="color: #0000ff;">end</span>,<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> order by </span><span style="color: #ff0000;">'</span>,_orderby,<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> limit </span><span style="color: #ff0000;">'</span>,<span style="color: #008000;">@startRow</span>,<span style="color: #ff0000;">'</span><span style="color: #ff0000;">,</span><span style="color: #ff0000;">'</span>,<span style="color: #008000;">@pageSize</span>); <span style="color: #0000ff;">prepare</span> strsql <span style="color: #0000ff;">from</span> <span style="color: #008000;">@strsql</span>; <span style="color: #0000ff;">execute</span> strsql; <span style="color: #0000ff;">deallocate</span> <span style="color: #0000ff;">prepare</span> strsql; <span style="color: #0000ff;">set</span> _totalcount <span style="color: #808080;">=</span> found_rows(); <span style="color: #0000ff;"> if</span> (_totalcount <span style="color: #808080;"><=</span> _pageSize) <span style="color: #0000ff;">then</span> |
1 2 3 4 5 6 7 |
<span style="color: #0000ff;">set</span> _pagecount <span style="color: #808080;">=</span> <span style="font-weight: bold; color: #800000;">1</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (_totalcount <span style="color: #808080;">%</span> _pageSize <span style="color: #808080;">></span> <span style="font-weight: bold; color: #800000;">0</span>) <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">set</span> _pagecount <span style="color: #808080;">=</span> _totalcount <span style="color: #808080;">/</span> _pageSize <span style="color: #808080;">+</span> <span style="font-weight: bold; color: #800000;">1</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">set</span> _pagecount <span style="color: #808080;">=</span> _totalcount <span style="color: #808080;">/</span> _pageSize; <span style="color: #0000ff;">end</span> <span style="color: #0000ff;">if</span>; <span style="color: #0000ff;">end</span> <span style="color: #0000ff;">if</span>; |
1 |
<span style="color: #0000ff;">if</span>(ifnull(_sumfields,<span style="color: #ff0000;">''</span>) <span style="color: #808080;"><></span> <span style="color: #ff0000;">''</span>) <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">set</span> <span style="color: #008000;">@sumsql</span> <span style="color: #808080;">=</span> contact(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">select </span><span style="color: #ff0000;">'</span>,_sumfields,<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> from </span><span style="color: #ff0000;">'</span>,_tables,<span style="color: #ff00ff;">case</span> ifnull(_<span style="color: #0000ff;">where</span>,<span style="color: #ff0000;">''</span>) <span style="color: #0000ff;">when</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">then</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">else</span> concat(<span style="color: #ff0000;">'</span><span style="color: #ff0000;"> where </span><span style="color: #ff0000;">'</span>,_<span style="color: #0000ff;">where</span>) <span style="color: #0000ff;">end</span>); <span style="color: #0000ff;">prepare</span> sumsql <span style="color: #0000ff;">from</span> <span style="color: #008000;">@sumsql</span>; <span style="color: #0000ff;">execute</span> sumsql; <span style="color: #0000ff;">deallocate</span> <span style="color: #0000ff;">prepare</span> sumsql; <span style="color: #0000ff;">end</span> <span style="color: #0000ff;">if</span>; <span style="color: #0000ff;">end</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">PROCEDURE</span> TransTest(<span style="color: #808080;">in</span> p1 <span style="color: #0000ff;">VARCHAR</span>(<span style="font-weight: bold; color: #800000;">20</span>),<span style="color: #808080;">in</span> p2 <span style="color: #0000ff;">VARCHAR</span>(<span style="font-weight: bold; color: #800000;">50</span>)) <span style="color: #0000ff;">BEGIN</span> <span style="color: #0000ff;">declare</span> err <span style="color: #0000ff;">int</span> <span style="color: #0000ff;">default</span> <span style="font-weight: bold; color: #800000;">0</span>; <span style="color: #008080;">/*</span><span style="color: #008080;">如果出现sql异常,则将err设置为1后继续执行后面的操作 </span><span style="color: #008080;">*/</span> <span style="color: #0000ff;">declare</span> <span style="color: #0000ff;">continue</span> handler <span style="color: #0000ff;">for</span> sqlexception <span style="color: #0000ff;">set</span> err<span style="color: #808080;">=</span><span style="font-weight: bold; color: #800000;">1</span>; <span style="color: #008080;">--</span><span style="color: #008080;"> 出错处理 </span> <span style="color: #0000ff;">set</span> autocommit <span style="color: #808080;">=</span> <span style="font-weight: bold; color: #800000;">0</span>; <span style="color: #0000ff;">insert</span> <span style="color: #0000ff;">into</span> sy_queryconfig(syq_id) <span style="color: #0000ff;">values</span>(p1); <span style="color: #0000ff;">insert</span> <span style="color: #0000ff;">into</span> sy_queryconfig(syq_id) <span style="color: #0000ff;">values</span>(p2); <span style="color: #0000ff;">if</span> err<span style="color: #808080;">=</span><span style="font-weight: bold; color: #800000;">1</span> <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">ROLLBACK</span>; <span style="color: #0000ff;">ELSE</span> <span style="color: #0000ff;">COMMIT</span>; <span style="color: #0000ff;">end</span> <span style="color: #0000ff;">if</span>; <span style="color: #0000ff;">END</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">PROCEDURE</span> TransTest2(<span style="color: #808080;">in</span> p1 <span style="color: #0000ff;">VARCHAR</span>(<span style="font-weight: bold; color: #800000;">20</span>),<span style="color: #808080;">in</span> p2 <span style="color: #0000ff;">VARCHAR</span>(<span style="font-weight: bold; color: #800000;">50</span>)) <span style="color: #0000ff;">BEGIN</span> <span style="color: #008080;">/*</span><span style="color: #008080;">只要发生异常就回滚</span><span style="color: #008080;">*/</span> <span style="color: #0000ff;">declare</span> <span style="color: #0000ff;">exit</span> handler <span style="color: #0000ff;">for</span> sqlexception <span style="color: #0000ff;">BEGIN</span> <span style="color: #0000ff;">ROLLBACK</span>; <span style="color: #008080;">/*</span><span style="color: #008080;">返回异常处理结果等其它操作</span><span style="color: #008080;">*/</span> <span style="color: #0000ff;">END</span>; START <span style="color: #0000ff;">TRANSACTION</span>; <span style="color: #0000ff;">insert</span> <span style="color: #0000ff;">into</span> sy_queryconfig(syq_id) <span style="color: #0000ff;">values</span>(p1); <span style="color: #0000ff;">insert</span> <span style="color: #0000ff;">into</span> sy_queryconfig(syq_id) <span style="color: #0000ff;">values</span>(p2); <span style="color: #0000ff;">COMMIT</span>; <span style="color: #0000ff;">END</span> |
from:http://www.cnblogs.com/peaceli/archive/2013/08/14/MYSQL.html