平时不怎么写存储过程,基本要用到的时候就到网上搜索,刚刚完成一个存储过程
大致作用是两个数据库,都有结构差不多的表,现在需要将一个库里该表同步到另一个库中,建了一个存储过程,并准备后面让这个存储过程自动2小时执行(这个用事件)
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‘);
insert into test1.klss_student(id, user_id, grade_num , pass_count, challenge_count, homework_count, total_integral, `level`, phone, badge_num, sex)
VALUES(UUID(), id1, grade1, 0, 0, 0, 0, 1, "", 0, 0);
end if;
end if;
#select user_name1;
set b=b+1;
if b = count then
leave loop_label;
end if;
end loop;
CLOSE cur_1;
end
---------------------
作者:Lionel_Medoo
来源:CSDN
原文:https://blog.csdn.net/juan0728juan/article/details/58587745
版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/HKROnline-SyncNavigator/p/10971493.html