if () syntax in a Trigger

Asked

Viewed 707 times

0

I’m having difficulties with the if() syntax in phpmyadmin. I have to create a TRIGGER to insert information in a log when the name of an animal table animals is changed. But there is a syntax error in if(). When I take out if it works. Can anyone help?

Follow the code:

DELIMITER $$
CREATE TRIGGER log_update 
AFTER UPDATE ON animais 
FOR EACH ROW

IF ( OLD.nome_animal <> NEW.nome_animal) THEN

INSERT INTO log_animais(codigo, momento, ocorrencia) VALUES ( OLD.cod_animal, now(), OLD.nome_animal  ) 

END
 $$

DELIMITER ; 

When I put the BEGIN, END and/or END IF it also error.

1 answer

1

There is an error in this which is exactly in END. The END is to close the BEGIN, if you want to close the IF use the END IF in his stead.

In a final way it would look like this:

CREATE TRIGGER log_update 
AFTER UPDATE ON animais 
FOR EACH ROW

BEGIN

 IF(OLD.nome_animal <> NEW.nome_animal) THEN

   INSERT 
    INTO log_animais(codigo, momento, ocorrencia) 
     VALUES (OLD.cod_animal, now(), OLD.nome_animal);

 END IF;

END
  • is that, I put so too, and so also of the error in the END. If you put the DEMILITER of the most error yet. No

  • @Luanaraujo What was the exact error? I tested this exactly on Mariadb and it worked normally.

  • "#1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near '' at line 11" Finally I got it like this: DELIMITER $$ CREATE TRIGGER log_update AFTER UPDATE ON animals FOR EACH ROW BEGIN IF(OLD.animal name_name <> NEW.animal) THEN INSERT INTO log_animals (codigo, moment, occurrence) VALUES (OLD.cod_animal, now(), OLD.animal name_name); END IF; END; $$ ; DELIMITER ; The strange thing was that the red X appeared on the code side, as it was wrong. But thank you so much for the strength @Inkeliz !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.