mysql 循环插入,MySQL插入带有While循环

I'm trying to create a bunch of records in my MySQL database. This is a one time creation so I am not trying to create a stored procedure. Here is my code:

BEGIN

SET i = 2376921001;

WHILE (i <= 237692200) DO

INSERT INTO `mytable` (code, active, total) values (i, 1, 1);

SET i = i+1;

END WHILE;

END

Here is the error:

[ERROR in query 1] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET i = 2376921001

WHILE (i <= 237692200) DO

INSERT INTO coupon (couponCod' at line 2

Execution stopped!

I have tried a Declare with the same results. Code below:

BEGIN

DECLARE i INT unsigned DEFAULT 2376921001;

WHILE (i <= 237692200) DO

INSERT INTO `mytable` (code, active, total) values (i, 1, 1);

SET i = i+1;

END WHILE;

END

The one other thing I have tried is with @i instead of just i. Same error.

Can anyone see what I am doing wrong?

解决方案

You have to put your code in a stored procedure. Example:

CREATE PROCEDURE myproc()

BEGIN

DECLARE i int DEFAULT 237692001;

WHILE i <= 237692004 DO

INSERT INTO mytable (code, active, total) VALUES (i, 1, 1);

SET i = i + 1;

END WHILE;

END

Alternatively, generate a list of INSERT statements using any programming language you like; for a one-time creation, it should be fine. As an example, here's a Bash one-liner:

for i in {2376921001..2376921099}; do echo "INSERT INTO mytable (code, active, total) VALUES ($i, 1, 1);"; done

By the way, you made a typo in your numbers; 2376921001 has 10 digits, 237692200 only 9.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值