1

I have a trigger which references a global variable that's coming from a delete query which sets the trigger off:

DELIMITER ;;

CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW 
BEGIN

    IF (SELECT @userID IS NULL) 
    THEN @userID := 0
    END IF;

END
;;

DELIMITER ;

For some reason I'm getting SQL error:

SQL 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 '@userID := 0

I don't understand this, it's a very basic IF statement, why doesn't it work?

UPDATE: I'm totally confused, I've tried this

IF TRUE
THEN TRUE
END IF;

and it still throws an error... Seriously?

UPDATE 2: SOLVED This actually works, however it is very weird

IF (SELECT @userID IS NULL) 
THEN SET userID = 0;
END IF;

Apparently you need a semicolon inside IF statement?

1 Answer 1

1
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW 
BEGIN
  IF (SELECT @userID IS NULL) 
  THEN 
    SET @userID = 0;
END IF;
END;
Sign up to request clarification or add additional context in comments.

1 Comment

Actually it still doesn't work, but if I put semicolon after 0 it does work. Very weird.

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.