无法确定在 MySQL 中创建存储过程的语法问题
Unable to determine syntax problem with creation of stored procedure in MySQL
我正在尝试创建以下存储过程 (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 行的语法,它似乎是正确的...?
每个命令都必须以 ;
结尾
另外,当应该用 IF THEN 重新排序时
最后你有很多事情要结束
所以下面不再报错
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
IF @targetPlayer > 4
then set @targetPlayer = 1;
end IF;
IF ((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 IF;
end while;
end //
DELIMITER ;
我正在尝试创建以下存储过程 (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 行的语法,它似乎是正确的...?
每个命令都必须以 ;
另外,当应该用 IF THEN 重新排序时
最后你有很多事情要结束
所以下面不再报错
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
IF @targetPlayer > 4
then set @targetPlayer = 1;
end IF;
IF ((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 IF;
end while;
end //
DELIMITER ;