2

I'm trying to change a value conditionally on my trigger, but I've got an error.

My trigger is:

CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW 
BEGIN
    IF NEW.per_base = 0 THEN
        SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
    END IF;
END;

but doesn't work.

2 Answers 2

1

You need to change the delimiter to something else than ;. Otherwise the trigger definition stops at the first ;

delimiter |
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW 
BEGIN
    IF NEW.per_base = 0 THEN
        SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
    END IF;
END
|
delimiter ;
Sign up to request clarification or add additional context in comments.

Comments

0

I also have the same problem. My SQL code before is just like this (without delimiter):

CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
    SET NEW.created=NOW();
END

Then, after i add the following DELIMITER // and close it with the same //

DELIMITER //
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
    SET NEW.created=NOW();
END //

It works. I hope it can help someone in the future...

Comments

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.