自学内容网 自学内容网

Mysql 循环插入数据

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()
 


原文地址:https://blog.csdn.net/qq_33715866/article/details/137551681

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!