Mysql学习MySQL存储过程例子(包含事务,输出参数,嵌套调用)

Mysql学习MySQL存储过程例子(包含事务,输出参数,嵌套调用),第1张

概述介绍《Mysql学习MySQL存储过程例子(包含事务,输出参数,嵌套调用)》开发教程,希望对您有用。

《MysqL学习MysqL存储过程例子(包含事务,输出参数,嵌套调用)》要点:
本文介绍了MysqL学习MysqL存储过程例子(包含事务,输出参数,嵌套调用),希望对您有用。如果有疑问,可以联系我们。

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,'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_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 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_Sync_ID;

end while; -- 循环结束
close cur; -- 关闭游标
end;

内存溢出PHP培训学院每天发布《MysqL学习MysqL存储过程例子(包含事务,输出参数,嵌套调用)》等实战技能,PHP、MysqL、liNUX、APP、Js,CSS全面培养人才。

总结

以上是内存溢出为你收集整理的Mysql学习MySQL存储过程例子(包含事务,输出参数,嵌套调用)全部内容,希望文章能够帮你解决Mysql学习MySQL存储过程例子(包含事务,输出参数,嵌套调用)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1156227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存