存储过程,游标。
游标例子:
转存数据从tb_user的姓名,手机号 转移到bf_user中去

-- 定义语法结束符号
delimiter //
-- 创建一个 名称为 p2 的存储过程
drop procedure if exists p2;
create procedure p2()
begin
declare total int;
-- 创建 用于接收游标值的变量
declare name_read , phone_read varchar(20) character set utf8;
-- 游标默认的标志
declare done int default 0;
-- 声明游标
declare cur cursor for select username, phone from tb_user;
-- 指定游标循环结束时的返回值
declare continue handler for not found set done = 1;
-- 打开游标
open cur;
-- 初始化 变量
set total = 0;
-- loop 循环
xxx:
loop
-- 根据游标当前指向的一条数据
fetch cur into name_read, phone_read;
-- 当 游标的返回值为 1 时 退出 loop循环
if done = 1 then
leave xxx;
end if;
-- todo
insert into bf_user values (name_read, phone_read);
-- 累计
set total = total + 1;
end loop;
-- 关闭游标
close cur;
-- 输出 累计的结果
select total;
end //
delimiter;
call p2();
运行之后


浙公网安备 33010602011771号