[Mysql] cross-database data synchronization

Usually do not write stored procedures, it is essential to use the time to search the Internet, has just completed a stored procedure

Role is roughly two databases, tables have similar structure, now a library that needs to be synchronized to another table in the library, built a stored procedure, and is ready to make the back of the stored procedure to perform automatic 2 hours (with this event )

 

begin
declare b int;
declare id1, user_name1, name1, password1, salt1 VARCHAR(100);
DECLARE userType int;
declare count, grade1 int;
DECLARE count1 int;
declare cur_1 cursor for
select id, username, `name`, `password`, salt, user_type from sys_user where `status`=1 and (user_type=4 or user_type=5);
SET b = 0;
select count(1) INTO count from sys_user where `status`=1;
#select count;
OPEN cur_1;
loop_label: loop
FETCH cur_1 INTO id1, user_name1, name1, password1, salt1, userType;

select count(1) into count1 from test1.sys_user where username=user_name1;
#select count1;
if count1 = 0 THEN
if userType=4 THEN
insert into test1.sys_user(id, username, `name`, `password`, salt, create_date, user_type, relative_school_id, `status`, avatar)
VALUES (id1,user_name1, name1, password1, salt1, now(), 3, '0140bc57-de2b-44a8-8039-e475d180db8a', 1, '/filesystem/headImg/default.jpg');
end if;
if userType=5 THEN
select tci.grade into grade1 from t_student_info tsi left join t_class_info tci on tsi.class_id=tci.id where tsi.user_id=id1 and tsi.`status`=1;
insert into test1.sys_user(id, username, `name`, `password`, salt, create_date, user_type, relative_school_id, `status`, avatar)
VALUES (id1,user_name1, name1, password1, salt1, now(), 4, '0140bc57-de2b-44a8-8039-e475d180db8a', 1, '/filesystem/headImg/default.jpg');
INTO test1.klss_student INSERT (ID, user_id, grade_num, pass_count, challenge_count, homework_count, total_integral, `level`, Phone, badge_num, Sex)
the VALUES (the UUID (), ID1, grade1, 0, 0, 0, 0,. 1, "", 0, 0);
End IF;
End IF;
#select user_name1;

SET + B = B. 1;
IF COUNT B = the then
Leave loop_label;
End IF;

End Loop;
the CLOSE cur_1;
End
------- --------------
author: Lionel_Medoo
source: CSDN
original: https: //blog.csdn.net/juan0728juan/article/details/58587745
copyright: This article is a blogger original article, reproduced Please attach Bowen link!

Guess you like

Origin www.cnblogs.com/HKROnline-SyncNavigator/p/10971493.html