由于业务需要,需要迁移客户表的几个字段到一张新表存储,在这里记录并学习。
使用软件为datagrip:
create procedure customerLinkman()
begin
# 定义变量名
declare new_customer_id varchar(32);
declare new_contacts varchar(32);
declare new_phone varchar(32);
declare new_email varchar(32);
# 定义游标
declare customer cursor for select id,contacts,phone,email from enterprise_customer;
# 指定游标循环结束时的返回值
declare done INT DEFAULT FALSE;
declare continue HANDLER for not found set done = true;
# 移除旧表数据
delete from enterprise_customer_linkman;
# 打开游标
open customer;
read_loop:loop
# 游标指向数据
fetch customer into new_customer_id,new_contacts,new_phone,new_email;
# 如果结束则关闭循环
if done then
leave read_loop;
end if;
insert into enterprise_customer_linkman values (uuid(),new_customer_id,new_contacts,new_phone,new_email,0,'76c6becb25392018e1c557c2bb5ab8b6',now(),'76c6becb25392018e1c557c2bb5ab8b6',now());
end loop;
close customer;
end;
call customerLinkman();
drop procedure customerLinkman;