Search code examples
mysqltriggersmysql-error-1064

Syntax error when creating a MySQL trigger


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?


Solution

  • Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:

    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 //