CREATE TABLE test_t(
role_id INT ,
menu_id INT ,
company_id INT
)
第一种:
INSERT INTO test_t(role_id,menu_id,company_id) SELECT 1000,id,1 FROM test WHERE product_id IS NULL
第二种:
DELIMITER //
CREATE PROCEDURE proc()
BEGIN
DECLARE role INT;
DECLARE menu INT;
DECLARE company INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
SELECT 1,id,1 FROM test WHERE product_id IS NULL;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO role,menu,company;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO test_t(role_id,menu_id,company_id) VALUES(role,menu,company);
END LOOP;
-- 关闭游标
CLOSE cur;
END //
CALL proc()