1

I have some problems trying to do a trigger.

Here is my MySQL query for the trigger:

delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
    SELECT round(2*livello*livello + 13.8*livello)
    FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
    WHILE (temp%6<>0) temp=temp+1;
    END WHILE;
    UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
    )
END IF;
END //

And the error is

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8

Does someone have an idea why I'm getting this error?

4

1 回答 1

2

THEN删除子句周围的括号。您的WHILE子句在语法上也不正确:

delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
    SELECT round(2*livello*livello + 13.8*livello)
    FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
    WHILE temp%6<>0 DO
      SET temp=temp+1;
    END WHILE;
    UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //
于 2013-10-27T18:28:49.497 回答