MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等(转)

view plaincopy to clipboardprint?

  1. drop procedure if exists pro_rep_shadow_rs; 
  2. delimiter | 
  3. ---------------------------------- 
  4. -- rep_shadow_rs 
  5. -- 用来处理信息的增加,更新和删除 
  6. -- 每次只更新上次以来没有做过的数据 
  7. -- 根据不同的标志位 
  8. -- 需要一个输出的参数, 
  9. -- 如果返回为0,则调用失败,事务回滚 
  10. -- 如果返回为1,调用成功,事务提交 
  11. -- 
  12. -- 测试方法 
  13. -- call pro_rep_shadow_rs(@rtn); 
  14. -- select @rtn; 
  15. ---------------------------------- 
  16. create procedure pro_rep_shadow_rs(out rtn int) 
  17. begin 
  18.     -- 声明变量,所有的声明必须在非声明的语句前面 
  19.     declare iLast_rep_sync_id int default -1; 
  20.     declare iMax_rep_sync_id int default -1; 
  21.     -- 如果出现异常,或自动处理并rollback,但不再通知调用方了 
  22.     -- 如果希望应用获得异常,需要将下面这一句,以及启动事务和提交事务的语句全部去掉 
  23.     declare exit handler for sqlexception rollback; 
  24.     -- 查找上一次的 
  25.     select eid into iLast_rep_sync_id from rep_de_proc_log where tbl='rep_shadow_rs'; 
  26.     -- 如果不存在,则增加一行 
  27. if iLast_rep_sync_id=-1 then 
  28.       insert into rep_de_proc_log(rid,eid,tbl) values(0,0,'rep_shadow_rs'); 
  29.       set iLast_rep_sync_id = 0; 
  30.     end if; 
  31.     -- 下一个数字 
  32.     set iLast_rep_sync_id=iLast_rep_sync_id+1; 
  33.     -- 设置默认的返回值为0:失败 
  34.     set rtn=0; 
  35.     -- 启动事务 
  36.     start transaction; 
  37.     -- 查找最大编号 
  38.     select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs; 
  39.     -- 有新数据 
  40. if iMax_rep_sync_id>=iLast_rep_sync_id then 
  41.         -- 调用 
  42.         call pro_rep_shadow_rs_do(iLast_rep_sync_id,iMax_rep_sync_id); 
  43.         -- 更新日志 
  44.         update rep_de_proc_log set rid=iLast_rep_sync_id,eid=iMax_rep_sync_id where tbl='rep_shadow_rs'; 
  45.     end if; 
  46.     -- 运行没有异常,提交事务 
  47.     commit; 
  48.     -- 设置返回值为1
  49.     set rtn=1; 
  50. end; 
  51. delimiter ; 
  52. drop procedure if exists pro_rep_shadow_rs_do; 
  53. delimiter | 
  54. --------------------------------- 
  55. -- 处理指定编号范围内的数据 
  56. -- 需要输入2个参数 
  57. -- last_rep_sync_id 是编号的最小值 
  58. -- max_rep_sync_id 是编号的最大值 
  59. -- 无返回值 
  60. --------------------------------- 
  61. create procedure pro_rep_shadow_rs_do(last_rep_sync_id int, max_rep_sync_id int) 
  62. begin 
  63.     declare iRep_operationtype varchar(1); 
  64.     declare iRep_status varchar(1); 
  65.     declare iRep_Sync_id int; 
  66.     declare iId int; 
  67.     -- 这个用于处理游标到达最后一行的情况 
  68.     declare stop int default 0; 
  69.     -- 声明游标 
  70.     declare cur cursor for select id,Rep_operationtype,iRep_status,rep_sync_id from rep_shadow_rs where rep_sync_id between last_rep_sync_id and max_rep_sync_id; 
  71.     -- 声明游标的异常处理,设置一个终止标记 
  72.     declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1; 
  73.     -- 打开游标 
  74.     open cur; 
  75.     -- 读取一行数据到变量 
  76.     fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id; 
  77.     -- 这个就是判断是否游标已经到达了最后 
  78. while stop <> 1 do
  79.         -- 各种判断 
  80. if iRep_operationtype='I' then 
  81.             insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id; 
  82.         elseif iRep_operationtype='U' then 
  83.         begin 
  84. if iRep_status='A' then 
  85.                 insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id; 
  86.             elseif iRep_status='B' then 
  87.                 delete from rs0811 where id=iId; 
  88.             end if; 
  89.         end; 
  90.         elseif iRep_operationtype='D' then 
  91.             delete from rs0811 where id=iId; 
  92.         end if;  
  93.         -- 读取下一行的数据  
  94.         fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id; 
  95.     end while;  -- 循环结束 
  96.     close cur; -- 关闭游标 
  97. end; 
  98. 闻香止步 收藏于:http://blog.csdn.net/java2000_net/archive/2009/03/24/4019982.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值