mysql 存储过程中使用游标实例

1 篇文章 0 订阅
1 篇文章 0 订阅

CREATE PROCEDURE `pro_insert_tsss_routemax`()
BEGIN
      -- 定义错误标识
            declare t_error INTEGER DEFAULT 0;  
      declare t_i int  DEFAULT 1; 
            declare t_mail_no varchar(1000);
            -- 游标结束的标志
            declare done int default 0;

            -- 定义游标
            declare cur_mail_no cursor for select a.mail_no from fs_route_back  a 
             group by a.mail_no having count(a.id)>1 limit 0,35000;
            declare CONTINUE HANDLER FOR SQLEXCEPTION  SET t_error=1;  
       -- 指定游标循环结束时的返回值 
      declare continue handler for not found set done = 1;

      --获得时间戳
      set @create_time = CAST((SELECT UNIX_TIMESTAMP(NOW()))*1000 AS DECIMAL(30));

      -- 数据记录
            open cur_mail_no;
            fetch cur_mail_no into t_mail_no;

     --记录日志表
      insert into test_routemax(p0,p1,p2,p5) values(@create_time,3,"routemax",t_i);
            -- while 循环
      while done != 1 do
                    fetch cur_mail_no into t_mail_no;
                         if done != 1 then
                                    START TRANSACTION;
                                                -- 执行处理
                                                insert into table_1
                                                select *  from  table_2;


                                    -- 如果错误则进行回滚
                                    IF t_error = 1 THEN  
                                            ROLLBACK; 
                                    ELSE 
                                    -- 更新执行结果 
                                            update test_routemax set p5=p5+1 where p0=@create_time and p2='routemax';      
                                            COMMIT;                     
                                    END IF;
                         end if;
            end while;
      -- 关闭游标
            close cur_mail_no;
end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值