实现简单搬表,使用游标循环
第一行 DELIMITER //和最后一行的//必须,否则遇到分号就报错
DELIMITER //create procedure movedata()BEGIN DECLARE Done INT DEFAULT 0; DECLARE v_id VARCHAR(30); /* 声明游标 */ DECLARE rs CURSOR FOR SELECT id FROM `new`.customer; /* 异常处理 */ DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1; /* 打开游标 */ OPEN rs; /* 逐个取出当前记录LingQi字段的值,需要进行最大值的判断 */ FETCH rs INTO v_id; /* 遍历数据表 */ REPEAT IF NOT Done THEN insert into `old`.customer(id,idcard_no) (select id+100000,idcard_no from `new`.customer where id = v_id); END IF; FETCH rs INTO v_id; UNTIL Done END REPEAT; /* 关闭游标 */ CLOSE rs; END //