我编辑了这个问题导致下面提到的答案,当使用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查询等。
我该如何解决这个错误?
答案 0 :(得分:6)
MySQL中的控制流语句(WHILE
,IF
等)可以在存储例程的上下文中使用 (过程,函数,触发器,事件) 。您不能在SQL批处理中单独使用它们。
<强> Flow Control Statements 强>
MySQL支持IF,CASE,ITERATE,LEAVE LOOP,WHILE和REPEAT构造,用于流程控制在存储程序中。