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

来源:互联网 发布:知白新书 编辑:程序博客网 时间:2024/05/01 14:37
  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.     -- 下一个数字  
  33.     set iLast_rep_sync_id=iLast_rep_sync_id+1;  
  34.     -- 设置默认的返回值为0:失败  
  35.     set rtn=0;  
  36.       
  37.     -- 启动事务  
  38.     start transaction;  
  39.     -- 查找最大编号  
  40.     select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs;  
  41.     -- 有新数据  
  42.     if iMax_rep_sync_id>=iLast_rep_sync_id then  
  43.         -- 调用  
  44.         call pro_rep_shadow_rs_do(iLast_rep_sync_id,iMax_rep_sync_id);  
  45.         -- 更新日志  
  46.         update rep_de_proc_log set rid=iLast_rep_sync_id,eid=iMax_rep_sync_id where tbl='rep_shadow_rs';  
  47.     end if;  
  48.       
  49.     -- 运行没有异常,提交事务  
  50.     commit;  
  51.     -- 设置返回值为1  
  52.     set rtn=1;  
  53. end;  
  54. |  
  55. delimiter ;  
  56. drop procedure if exists pro_rep_shadow_rs_do;  
  57. delimiter |  
  58. ---------------------------------  
  59. -- 处理指定编号范围内的数据  
  60. -- 需要输入2个参数  
  61. -- last_rep_sync_id 是编号的最小值  
  62. -- max_rep_sync_id 是编号的最大值  
  63. -- 无返回值  
  64. ---------------------------------  
  65. create procedure pro_rep_shadow_rs_do(last_rep_sync_id int, max_rep_sync_id int)  
  66. begin  
  67.     declare iRep_operationtype varchar(1);  
  68.     declare iRep_status varchar(1);  
  69.     declare iRep_Sync_id int;  
  70.     declare iId int;  
  71.     -- 这个用于处理游标到达最后一行的情况  
  72.     declare stop int default 0;  
  73.     -- 声明游标  
  74.     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;  
  75.     -- 声明游标的异常处理,设置一个终止标记  
  76.     declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;  
  77.       
  78.     -- 打开游标  
  79.     open cur;  
  80.       
  81.     -- 读取一行数据到变量  
  82.     fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;  
  83.     -- 这个就是判断是否游标已经到达了最后  
  84.     while stop <> 1 do  
  85.         -- 各种判断  
  86.         if iRep_operationtype='I' then  
  87.             insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;  
  88.         elseif iRep_operationtype='U' then  
  89.         begin  
  90.             if iRep_status='A' then  
  91.                 insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;  
  92.             elseif iRep_status='B' then  
  93.                 delete from rs0811 where id=iId;  
  94.             end if;  
  95.         end;  
  96.         elseif iRep_operationtype='D' then  
  97.             delete from rs0811 where id=iId;  
  98.         end if;   
  99.           
  100.         -- 读取下一行的数据   
  101.         fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;  
  102.     end while;  -- 循环结束  
  103.     close cur; -- 关闭游标  
  104.  end;  
0 0
原创粉丝点击