我正在尝试编写mySQL tigger,但如果没有语法错误,就无法将代码放入phpMyAdmin MySQL中.有人能帮助我吗?
桌子:
我的触发器:
CREATE TRIGGER after_jeu_insert ON jeu
AFTER INSERT
AS
BEGIN
DECLARE @gameid int, @oldTurn int, @newTurn int
SELECT @gameid=idpartie FROM INSERTED
SELECT @oldturn=tour FROM partie WHERE idpartie=@gameid
IF(@oldTurn IS NULL)
{
SET @newTurn = 1
}
ELSE
{
IF(@oldTurn==1)
SET @newTurn = 2
ELSE
SET @newTurn = 1
}
UPDATE partie SET tour = @newTurn, derniercoup = NOW() WHERE idpartie = @gameid
END
我没有发现错误,如果有人可以帮助我,那就太好了.
谢谢
解决方法:
有很多错误可以纠正.
>没有结束语.
>不遵循语法顺序.
>变量声明不正确.
>从列中选择变量不正确.
>不支持基于括号的时钟.
>插入的表格仅包含一行吗?否则,您需要一个where子句或限制.
>等
您最好多学习.
请参考Trigger Syntax and Examples,以更好地理解.
如下更改代码,如果在数据库对象上一切正常,它可能会起作用.
drop trigger if exists after_jeu_insert;
delimiter //
CREATE TRIGGER after_jeu_insert after insert ON jeu for each row
BEGIN
DECLARE _game_id int;
DECLARE _old_turn int;
DECLARE _new_turn int;
-- following line may not require limit clause
-- if used proper where condition.
SELECT idpartie into _game_id FROM INSERTED limit 1;
SELECT tour into _old_turn FROM partie WHERE idpartie = _game_id;
IF _old_turn IS NULL then
SET _new_turn = 1;
ELSIF _old_turn = 1 then
SET _new_turn = 2;
ELSE
SET _new_turn = 1;
END IF;
UPDATE partie
SET tour = _new_turn
, derniercoup = NOW()
WHERE idpartie = _game_id;
END;
//
delimiter;