0

I'm trying to create a trigger where I set a colunm to 0 if another column is set to 0 and 1 if it's set higher than 0. This is what I wrote

CREATE TRIGGER livre_disponible BEFORE INSERT ON livres
FOR EACH ROW
BEGIN
 IF NEW.nb_exemplaires=0 THEN
 SET NEW.disponible=0;
 ELSEIF NEW.nb_exemplaires>=1 THEN
 SET NEW.disponible=1;
 END IF
END

The error is said to be on line 5 which is the first "SET".

I this the correct syntax to do it?

mustaccio
28.6k24 gold badges60 silver badges77 bronze badges
asked Mar 16, 2017 at 11:17

1 Answer 1

1

I didn't find anything wrong with your statement, you need perhaps DELIMITERs

DELIMITER $$
CREATE
 TRIGGER `livre_disponible` BEFORE INSERT ON `livres` 
 FOR EACH ROW 
 BEGIN
 IF NEW.nb_exemplaires=0 THEN
 SET NEW.disponible=0;
 ELSEIF NEW.nb_exemplaires>=1 THEN
 SET NEW.disponible=1;
 END IF;
 END;
$$
DELIMITER ;

Updated : you missed also a ';' ==> END IF; END;

answered Mar 16, 2017 at 14:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.