SQL查询中的错误

时间:2015-05-28 14:30:45

标签: php mysql

我有一个查询应该将随机数据添加到我的表中,但它不起作用。

我的查询如下:

DECLARE @i INT;
SET @i = 0;
WHILE (@i < 25000) DO
BEGIN
INSERT INTO ak_class (class_name, class_description)
VALUES (CONCAT('Rose', RAND()*250000),
CONCAT('Roses are awesome', RAND()*250000));
SET @i = @i + 1;
END WHILE;

显示的错误:

Error Code : 1064
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 'DECLARE @i INT' at line 1

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

(0 row(s) affected)
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

Error Code : 1064
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 'WHILE (@i < 25000) DO
BEGIN
INSERT INTO ak_class (class_name, class_description)' at line 1

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

(0 row(s) affected)
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

Error Code : 1064
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 'END WHILE' at line 1

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

1 个答案:

答案 0 :(得分:0)

MySQL不支持执行存储过程代码的匿名块。

您需要创建一个包含该代码的存储过程,然后调用它。

创建程序:

DELIMITER $$

DROP PROCEDURE IF EXISTS insert_random_rows $$

CREATE PROCEDURE insert_random_rows () 
BEGIN
DECLARE @i INT;
SET @i = 0;
WHILE (@i < 25000) DO
BEGIN
INSERT INTO ak_class (class_name, class_description)
VALUES (CONCAT('Rose', RAND()*250000),
CONCAT('Roses are awesome', RAND()*250000));
SET @i = @i + 1;
END WHILE;
END $$
DELIMITER ;

你现在需要打电话:

CALL insert_ten_rows();

这应该适合你。