创建表:
sql语句(以100条为例):
DROP PROCEDURE IF EXISTS test_insert; CREATE PROCEDURE test_insert() BEGIN DECLARE i INT; // 也可以这样写 DECLARE i INT DEFAULT 1; SET i=1; WHILE i <= 100 DO INSERT INTO `course` (`name`) VALUES(CONCAT('课程',i)); SET i=i+1; END WHILE; END; CALL test_insert();
运行结果:
另一个示例:
DROP PROCEDURE IF EXISTS test_insert; CREATE PROCEDURE test_insert() BEGIN DECLARE x INT DEFAULT 1; DECLARE y INT DEFAULT 1; DECLARE z INT DEFAULT 1; WHILE z<=80000 DO INSERT INTO `student_score` (course_id,stu_id,score) VALUES(x,y,z); SET x=x+1; SET y=y+1; SET z=z+1; SET x = if(x > 101, 1, x+1); SET y = if(y > 80001, 1, y+1); END WHILE; END; CALL test_insert();
参考:https://dandelioncloud.cn/article/details/1494345391373099010