我有以下查询循环数据块并将其复制到另一个表。我使用label的目标之一是在满足某些条件后有一个退出循环的机制:
SET @nth = 0;
SET @size = 1000 * 1;
SET @offset = @nth * @size;
SET @count = 7120;
lable1: LOOP
PREPARE select_stmt FROM
"INSERT INTO foo.`track_new` (
`...`
)
SELECT
`...`
FROM foo.`track` a
WHERE ...
ORDER BY ...
LIMIT ?, ?";
EXECUTE select_stmt USING @offset, @size;
IF @offset + @size > @count THEN LEAVE lable1;
END IF;
...
END LOOP;
但是我收到了这些错误(删除了成功查询的消息):
Query: lable1: LOOP PREPARE select_stmt FROM "INSERT INTO gps.`track_new` ( `id`, `unit_id`, `gps_time`, `added_when`, `latit...
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 'lable1: LOOP
PREPARE select_stmt FROM
"INSERT INTO gps.`track_new` (
`id`,
' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
--------------------------------------------------
...
Query: IF @offset + @size > @count THEN leave lable1
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 'IF @offset + @size > @count THEN leave lable1' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
--------------------------------------------------
我已经检查过标签的语法,看起来很好。 我还检查了mysql中循环和其他控件结构的语法,但我无法弄清楚是什么问题。
修改
我注意到行EXECUTE select_stmt USING @offset, @size;
被执行但后来循环不再重复。