MySql循环插入数据

DELIMITER $$

USE `test`$$

DROP PROCEDURE IF EXISTS `score`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `score`()
BEGIN
    declare i int;
    declare n varchar(20);
    declare s int;
    declare done int default -1;
    declare myCursor cursor for select id from test.abc;
    declare C2 cursor for select `name` from test.abc;
    declare continue handler for not found set done = 1;
    set s = 60;
    open myCursor;
    open C2;
    myLoop:loop
    FETCH myCursor INTO i;
    fetch C2 into n;
    IF done = 1
    THEN 
    leave myLoop;
    end if;
    insert into score values(n,i,s);
    set s = s + 1;
    end loop myLoop;
    close myCursor;
    close C2;

    END$$

DELIMITER ;
DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    PROCEDURE `test`.`new`()
    /*LANGUAGE SQL
    | [NOT] DETERMINISTIC
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
    | SQL SECURITY { DEFINER | INVOKER }
    | COMMENT 'string'*/
    BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 10 DO
    INSERT INTO test.abc(`name`,phone,createTime) VALUES('888','18825175391',UNIX_TIMESTAMP(NOW()));
    SET i = i + 1;
    END WHILE;

    END$$

DELIMITER ;

 

posted @ 2021-11-20 23:13  #天羽Owl  阅读(566)  评论(0编辑  收藏  举报