phpmyadmin 无法确定在MySQL中创建存储过程的语法问题

ibps3vxo  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(166)

下面是一个存储过程(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行(declare turnHasBeenChanged tinyint(1)default 0),以防它的语法导致后续行失败,并在它的末尾添加了一个分号,这在该行中断了它,所以它不是这样。
然后,我对照https://www.oreilly.com/library/view/mysql-cookbook/0596001452/ch01s15.html验证了第5行的语法,它似乎是正确的......?

zfycwa2u

zfycwa2u1#

每个命令都必须以;结尾
还应替换为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 ;

相关问题