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

create procedure pro_rep_shadow_rs(out rtn int)  
begin  
    -- 声明变量,所有的声明必须在非声明的语句前面  
    declare iLast_rep_sync_id int default -1;  
    declare iMax_rep_sync_id int default -1;  
    -- 如果出现异常,或自动处理并rollback,但不再通知调用方了  
    -- 如果希望应用获得异常,需要将下面这一句,以及启动事务和提交事务的语句全部去掉  
    declare exit handler for sqlexception rollback;  
    -- 查找上一次的  
    select eid into iLast_rep_sync_id from rep_de_proc_log where tbl='rep_shadow_rs';  
    -- 如果不存在,则增加一行  
    if iLast_rep_sync_id=-1 then  
      insert into rep_de_proc_log(rid,eid,tbl) values(0,0,'rep_shadow_rs');  
      set iLast_rep_sync_id = 0;  
    end if;  
      
    -- 下一个数字  
    set iLast_rep_sync_id=iLast_rep_sync_id+1;  
    -- 设置默认的返回值为0:失败  
    set rtn=0;  
      
    -- 启动事务  
    start transaction;  
    -- 查找最大编号  
    select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs;  
    -- 有新数据  
    if iMax_rep_sync_id>=iLast_rep_sync_id then  
        -- 调用  
        call pro_rep_shadow_rs_do(iLast_rep_sync_id,iMax_rep_sync_id);  
        -- 更新日志  
        update rep_de_proc_log set rid=iLast_rep_sync_id,eid=iMax_rep_sync_id where tbl='rep_shadow_rs';  
    end if;  
      
    -- 运行没有异常,提交事务  
    commit;  
    -- 设置返回值为1 
    set rtn=1;  
end;  
|  
delimiter ;  
drop procedure if exists pro_rep_shadow_rs_do;  
delimiter |  
---------------------------------  
-- 处理指定编号范围内的数据  
-- 需要输入2个参数  
-- last_rep_sync_id 是编号的最小值  
-- max_rep_sync_id 是编号的最大值  
-- 无返回值  
---------------------------------  
create procedure pro_rep_shadow_rs_do(last_rep_sync_id int, max_rep_sync_id int)  
begin  
    declare iRep_operationtype varchar(1);  
    declare iRep_status varchar(1);  
    declare iRep_Sync_id int;  
    declare iId int;  
    -- 这个用于处理游标到达最后一行的情况  
    declare stop int default 0;  
    -- 声明游标  
    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;  
    -- 声明游标的异常处理,设置一个终止标记  
    declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;  
      
    -- 打开游标  
    open cur;  
      
    -- 读取一行数据到变量  
    fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;  
    -- 这个就是判断是否游标已经到达了最后  
    while stop <> 1 do 
        -- 各种判断  
        if iRep_operationtype='I' then  
            insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;  
        elseif iRep_operationtype='U' then  
        begin  
            if iRep_status='A' then  
                insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;  
            elseif iRep_status='B' then  
                delete from rs0811 where id=iId;  
            end if;  
        end;  
        elseif iRep_operationtype='D' then  
            delete from rs0811 where id=iId;  
        end if;   
          
        -- 读取下一行的数据   
        fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;  
    end while;  -- 循环结束  
    close cur; -- 关闭游标  
 end;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值