我试图编写一个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
我没有发现错误,如果有人可以帮助我,那将是非常好的。 感谢
答案 0 :(得分:2)
要纠正的错误很多。
inserted
只包含一行吗?否则,您需要where
子句或limit
。你最好学习更多东西 有关更好的理解,请参阅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;