mysql循环错误代码#1064内存[修订]

时间:2013-11-21 04:23:25

标签: mysql while-loop syntax-error

我编辑了这个问题导致下面提到的答案,当使用while和begin时,它应该在一个存储过程中。

所以现在我在存储过程中运行它,但我仍然收到错误。

counter;
while counter < 2
begin
GOTO counter

SELECT var1 AS LOCATION,
  ROUND(sum(CASE WHEN t2.tagid = var2 THEN t2.floatvalue ELSE 0 END), 2) AS MR
  ROUND(sum(CASE WHEN t2.tagid = var3 THEN t2.floatvalue ELSE 0 END), 2) AS Flw,
  ROUND(sum(CASE WHEN t2.tagid = var4 THEN t2.floatvalue ELSE 0 END), 2) AS Prs,
  ROUND(sum(CASE WHEN t2.tagid = var5 THEN t2.floatvalue ELSE 0 END), 2) AS Lvl
FROM table2 t2

INNER JOIN table1  t1
  ON t1.id = t2.tagid

WHERE t2.t_stamp = (SELECT MAX(t2.t_stamp) 
                    FROM table2  t2 
                    WHERE t1.id = t2.tagid)
counter = counter+1
end

1:
var1
var2
var3
var4
var5

2:
var1
var2
var3
var4
var5

这是我得到的错误

MySQL said: #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 'begin GOTO counter SELECT var1 AS LOCATION, ROUND(sum(CASE WHEN t2.tagid' at line 2

我没有包含CREATE PROCEDURE因为我看到了一个帖子,其中更容易进入ROUTINES面板并粘贴没有CREATE,DELIMITER等的SQL查询等。 我该如何解决这个错误?

1 个答案:

答案 0 :(得分:6)

MySQL中的控制流语句(WHILEIF等)可以在存储例程的上下文中使用 (过程,函数,触发器,事件) 。您不能在SQL批处理中单独使用它们。

  

<强> Flow Control Statements
  MySQL支持IF,CASE,ITERATE,LEAVE LOOP,WHILE和REPEAT构造,用于流程控制在存储程序中