drop procedure if exists pro_rep_shadow_rs;
delimiter |
----------------------------------
-- rep_shadow_rs
-- 用来处理信息的增加,更新和删除
-- 每次只更新上次以来没有做过的数据
-- 根据不同的标志位
-- 需要一个输出的参数,
-- 如果返回为0,则调用失败,事务回滚
-- 如果返回为1,调用成功,事务提交
--
-- 测试方法
-- call pro_rep_shadow_rs(@rtn);
-- select @rtn;
----------------------------------
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;
MySQL存储过程例子(包含事务,输出参数,嵌套调用)
2019-11-11 15:33mysql教程网 Mysql
MySQL存储过程例子,包含事务,输出参数,嵌套调用,学习mysql存储过程的朋友可以参考下。
延伸 · 阅读
- 2019-11-11MySQL与SQL的触发器的不同写法
- 2019-11-11教会你完全搞定MySQL数据库 轻松八句话
- 2019-11-11mysql时间戳转成常用可读时间格式的两种方法
- 2019-11-11mysql query browser中文乱码的解决方法
- 2019-11-11配置MySQL与卸载MySQL实例操作
- 2019-11-11MySql 安装时的1045错误
精彩推荐
- Mysql
MySQL 数据库常用命令 简单超级实用版
MySQL 数据库常用命令,都是一些比较基础的东西,更多的命令可以查看相关文章里面的文字。 ...
- Mysql
MySQL 8.0.13设置日期为0000-00-00 00:00:00时出现的问题解决
这篇文章主要介绍了MySQL 8.0.13设置日期为0000-00-00 00:00:00时出现的问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...
- Mysql
MySQL数据库主机127.0.0.1与localhost区别
这篇文章主要介绍了MySQL主机127.0.0.1与localhost区别总结,需要的朋友可以参考下...
- Mysql
Mysql如何避免全表扫描的方法
如果MySQL需要做一次全表扫描来处理查询时,在 EXPLAIN 的结果中 type 字段的值是 ALL。 ...
- Mysql
mysql基础架构教程之查询语句执行的流程详解
这篇文章主要给大家介绍了关于mysql基础架构教程之查询语句执行流程的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的...
- Mysql
关于case when语句的报错问题详解
SQL CASE 表达式是一种通用的条件表达式,类似于其它语言中的 if/else 语句,下面这篇文章主要给大家介绍了关于case when语句的报错问题的相关资料,文中通...
- Mysql
MySQL与SQL的触发器的不同写法
当在SQL、MySQL数据库中一张表中插入一条记录时,触动触发器,使同一数据库的另一张表插入相同记录。 ...
- Mysql
MySQL的数据类型和建库策略分析详解
无论是在小得可怜的免费数据库空间或是大型电子商务网站,合理的设计表结构、充分利用空间是十分必要的。这就要求我们对数据库系统的常用数据类型...