1

I am having difficulty creating the following trigger in MySQL:

CREATE TRIGGER TRG_COMPLETE_REMINDER
AFTER UPDATE ON reminders
FOR EACH ROW
BEGIN
    DECLARE
        new_date DATE;
    IF (NEW.complete = 1 AND recurrence <> 'NONE') THEN
        CASE recurrence
            WHEN '1 WEEK' THEN
                SELECT INTO new_date NEW.date + INTERVAL 7 DAY;
            WHEN '1 MONTH' THEN
                SELECT INTO new_date  NEW.date + INTERVAL 1 MONTH;
            WHEN '3 MONTH' THEN
                SELECT INTO new_date  NEW.date + INTERVAL 3 MONTH;
        END CASE;

        INSERT INTO reminders (description, date, userID, complete, recurrence)
        VALUES (NEW.description, new_date, NEW.userID, 0, NEW.recurrence);
    END IF;
END;

The issue seems to be occurring where I attempt to declare new_date. MySQL returns the following error message:

#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 'INTO new_date NEW.date + INTERVAL 7 DAY' at line 8

I would like this trigger to create a new reminder record when the complete attribute changes to 1. The new record should have a date greater than the original record, depending on the value stored in recurrence.

11
  • Your select is the wrong way round it should be SELECT value INTO variable - I don't know if that solves everything. You could also do SET new_date = NEW.date + INTERVAL 7 DAY; Commented Jun 6, 2016 at 15:54
  • @PaulF You are right, but this does not fix the DECLARE issue. Commented Jun 6, 2016 at 15:57
  • What error do you get on the DECLARE - the error you reported was related to the incorrect SELECT INTO syntax only. Commented Jun 6, 2016 at 16:00
  • 1
    Have you changed the delimiter from ; Commented Jun 6, 2016 at 16:03
  • 1
    @ÁlvaroGonzález there were two issues in the problem though, so not completely duplicate - originally OP reported the SELECT INTO syntax error before the edit to show the error at line #6. Commented Jun 6, 2016 at 16:10

2 Answers 2

1

Two corrections fixed the issue:

  1. Correcting the order of clauses in the SELECT statement (i.e. moving INTO new_date after SELECT new_date + INTERVAL 7 DAY)
  2. Adding DELIMITER // before creating the trigger
Sign up to request clarification or add additional context in comments.

Comments

1

Rather than use a variable you can use a CASE expression right in the INSERT statement:

CREATE TRIGGER TRG_COMPLETE_REMINDER
  AFTER UPDATE ON reminders
  FOR EACH ROW
BEGIN
  IF (NEW.complete = 1 AND NEW.recurrence <> 'NONE') THEN
    INSERT INTO reminders
     (description,
      date,
      userID,     complete, recurrence)
    VALUES
     (NEW.description, 
      CASE NEW.recurrence
        WHEN '1 WEEK' THEN
          NEW.date + INTERVAL 7 DAY
        WHEN '1 MONTH' THEN
          NEW.date + INTERVAL 1 MONTH
        WHEN '3 MONTH' THEN
          NEW.date + INTERVAL 3 MONTH
        ELSE
          NULL
      END,
      NEW.userID, 0,        NEW.recurrence);
  END IF;
END;

Best of luck.

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.