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?

1 Answer 1

2

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 //
Sign up to request clarification or add additional context in comments.

4 Comments

it'sstill not working, same error #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 'temp=temp+1; END WHILE; UPDATE strutture SET produzione=temp WHERE cit' at line 9
@Sanci: Well, for a start that's not the same error. Now you need to look at the WHILE clause: in particular, it should be WHILE temp%6<>0 DO SET temp=temp+1; END WHILE;. See my edit.
Yep, we're near the solution. Now there's this last error i suppose, because it's on last line: #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 '//' at line 14
I removed the final // and now it works! Thanky you, you saved my day !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.