Batch Insert SQL query in MySQL
I just keep getting the error 1064. I searched how to do while loops then declare local variables etc. and I don't see what I'm doing wrong. I tried to do it without the ";" and I tired setting the delimiter as "|" to be able to use ";" as a separator between lines (I read something somwhere that kind of said it could be the way to do it?..)
I'm trying to do that query on 开发者_StackOverflow中文版PhpMyAdmin and my MySql version is 5.1.36
I'm not going to explain what I'm trying as I believe it is easy to understand by simply reading my query below.
BEGIN
DECLARE v1 INT DEFAULT 0;
DECLARE v2 VARCHAR(10);
WHILE v1 < 20 DO
SET v2 = CONCAT('Test ', CAST(v1 AS CHAR(2)));
INSERT INTO news(title,date, message) VALUES(v2, NOW(), v2);
SET v1 = v1 + 1;
END WHILE;
END;
MySql only allows compound statements using the BEGIN...END
tag inside stored programs.
From the Docs:
BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs
The first thing i can see is a small syntax error within the CAST parameters.
You have: CAST(v1 ASCHAR(2))
You need: CAST(v1 AS CHAR(2))
See http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
Change
DECLAREv2 VARCHAR(10);
to
DECLARE v2 VARCHAR(10);
精彩评论