mysql数据库中同步两个表中的数据的问题

写个存储过程,方便以后调用,下面是手动写的不对的地方自己改改;
写得累死了,还加了注释 不加分 天理难容 累觉不爱

create or replace procedure publish_users is

cursor c_record_players is
select * from players;

v_cur c_record_players%rowtype;
v_db players%rowtype;

begin
--打开游标,处理结果集
open c_record_players;
loop
fetch c_record_players
into v_cur;
exit when c_record_players %notfound;
--如果是刚开始 users表是空的就插入数据
begin
select count(*)
into v_record_num
from users t
where t.id=v_cur.id;

if v_record_num=0 then
insert into users
( Ip ,
username)
values
( v_cur.ipadress ,
v_cur.playername
);

else
--方便以后处理如果users不是空的那么就更新数据
update users
set ip=v_cur.ipadress ,
username=v_cur.playername ,
where username=v_cur.playername;

end if;
--数据量大的话 每5000条提交一次
if mod(c_record_players%rowcount,5000)=0 then
commit;
end if;

end loop;
close c_record_players;
end publish_users;

猜你喜欢

转载自www.cnblogs.com/mynale/p/11018456.html