- 业务需求:通过遍历A表,获得该表所有的主键id,循环为B表插入数据
DROP PROCEDURE IF EXISTS sumVotePro;
delimiter
CREATE PROCEDURE sumVotePro()
BEGIN
DECLARE $shopId int(11);
DECLARE $now int default unix_timestamp();
DECLARE done INT DEFAULT FALSE;
DECLARE cur_columns CURSOR FOR select id from tab_a;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
OPEN cur_columns;
read_loop: LOOP
FETCH NEXT from cur_columns INTO $shopId;
IF done THEN
LEAVE read_loop;
END IF;
insert into tab_b values
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null),
(null, 0, $shopId, $now,null);
END LOOP;
CLOSE cur_columns;
END
call sumVotePro();