【发布时间】:2026-01-13 12:30:01
【问题描述】:
我正在尝试创建以下存储过程 (XAMPP > phpMyAdmin):
DROP PROCEDURE IF EXISTS changeTurn;
DELIMITER //
create procedure changeTurn(in currentGameID bigint(20))
begin
declare turnHasBeenChanged tinyint(1) default 0
select @targetPlayer := player_number from game_players where gameID = currentGameID and current_turn = 1
set @targetPlayer = @targetPlayer + 1
update game_players set current_turn = 0 where gameID = currentGameID and current_turn = 1
while turnHasBeenChanged = 0 do
case
when @targetPlayer > 4
then set @targetPlayer = 1
end
case
when (select forfeit_next_turn from game_players where gameID = currentGameID and player_number = @targetPlayer) = 1
then
update game_players set forfeit_next_turn = 0 where gameID = currentGameID and player_number = @targetPlayer
set @targetPlayer = @targetPlayer + 1
else
update game_players set current_turn = 1 where gameID = currentGameID and player_number = @targetPlayer
set turnHasBeenChanged = 1
end
end while
end
end //
DELIMITER ;
无论有没有分隔符,我都会遇到以下问题:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'select @targetPlayer := player_number from game_players where gameID = curren...' at line 5
很遗憾,这不是很具体。
在将这个过程拼凑在一起时,我确保我对每个部分都使用了正确的语法。但现在它完全,它很无聊。请你能给我一些关于为什么会出错的方向吗?
我检查了第 4 行(声明 turnHasBeenChanged tinyint(1) 默认为 0),以防它的语法导致后续行失败,并在其末尾添加了一个分号,并在该行中断了它,所以不是那样的。
然后我根据https://www.oreilly.com/library/view/mysql-cookbook/0596001452/ch01s15.html 验证了第 5 行的语法,它似乎是正确的...?
【问题讨论】:
-
您在上一行的末尾缺少
;。 -
您在整个过程中都缺少很多
;字符。在每条语句的末尾都需要;。 -
@Barmar 我之前有分号,这些行给了我同样的错误。例如。如果我在第 4 行的末尾添加一个分号,它会在第 4 行中断并出现相同的错误。
-
您必须在每条语句之后添加它们。否则,您将在下一条语句中收到该错误。
标签: mysql variables syntax phpmyadmin mysql-error-1064