1

Running on MySQL 5.5.9 with InnoDB.

I created the following trigger:

CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products

FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE   id = new.id
    AND current = 1
    AND autonumber <> new.autonumber
END;

MySQLWorkbench shows a syntax error on the last line, and executing this throws the following error

Error Code: 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 'END' at line 11

Where is my error?

2 Answers 2

5

http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html

DELIMITER |

CREATE TRIGGER TRIGGER_Products_Insert AFTER INSERT ON Products
    FOR EACH ROW BEGIN
        UPDATE Products
        SET current = 0
        WHERE   id = new.id
            AND current = 1
            AND autonumber <> new.autonumber;
    END;
|

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

3 Comments

This works, but I have no clue why the DELIMITER stuff is necessary.
Because BEGIN ... END is a "body" of trigger. So, it is must be a part of current query. ; is a delimeter by default, so when MySQL reaches it, it thinks that this is the end of query and cannot parse it. Also, you must put delimeters in "body" of trigger because it would be invalid in another way. So, to meet this requirements, we need to use two different delimeters in "body" of trigger and in query for itself. Sorry, my english is too bad to describe this not obvious thing better...
This better described here: dev.mysql.com/doc/refman/5.5/en/begin-end.html (third paragraph, starts with "Use of multiple statements requires that a client is able")
0

You are missing a ; before your END keyword:

CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products

FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE   id = new.id
    AND current = 1
    AND autonumber <> new.autonumber;
END;

1 Comment

Doesn't work, gives errors on both the last and the second last line now.

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.