0

I have a database already in use on a server (MySQL Community Server 5.5.40-cll) & I'm trying to add a trigger to it but I keep getting the below 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 '' at line 3 

I'm trying to set a trigger to make sure that end_date is not before start_date, here's what I'm currently using.

CREATE TRIGGER `Meets insert` BEFORE INSERT ON `meets`
  FOR EACH ROW 
    IF DATEDIFF(NEW.end_date, NEW.start_date) < 0 THEN
      signal sqlstate '45000' set message_text = 'start_date must be before end_date';
    END IF;

I've had a look in the MySQL documentation for 5.5 and it seems like everything I'm doing is supported by this version and I'm using it correctly.

I'm sure the if statement is correct as I asked another similar question earlier here on Stack Overflow about a separate problem I was having which worked fine. Although that was on a local development database where as this is on a server.

If anyone can spot what I'm doing wrong or has an idea of what to check it will be appreciated.

Thought I'd point out the server I'm making this change on isn't a production one.

1 Answer 1

1

It appears to only be a lack of a DELIMTER wrapper. Try

delimiter $$
CREATE TRIGGER `Meets insert` BEFORE INSERT ON `meets`
  FOR EACH ROW 
    IF DATEDIFF(NEW.end_date, NEW.start_date) < 0 THEN
      signal sqlstate '45000' set message_text = 'start_date must be before end_date';
    END IF;
$$
delimiter ;

which gets past the Error 1064

See the bottom of This Post for DELIMITER verbiage.

Sign up to request clarification or add additional context in comments.

2 Comments

That seems to have solved, I've not come across that before.
it's ridiculous and people waste hours on it. From the server consuming part of it though, it needs to know where one thing begins and ends (the outer envelope of the trigger, stored proc, event etc), and the commands within it

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.