Question

Hello I have a problem with this trigger:

CREATE TRIGGER sprLog AFTER UPDATE ON Users FOR WACH ROW
BEGIN
     IF OLD.wl < NEW.wl THEN
        IF NEW.wl = 3 THEN
           INSERT INTO blocked(uid, time) VALUE(NEW.ID, 15);
           UPDATE Users SET state=3 WHERE ID=NEW.ID;
        END IF;
     END IF;
END;

Now when i try to execute this query: UPDATE Users SET wl=3 WHERE ID=1

I have error:

#1442 - Can't update table 'Users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

I hope that you understand from trigger what I want to do. I know that this trigger causes itself, but I don't have idea how to do that in other way. I tried using two trigger one After Update on Users and second After Insert On blocked, but the problem were the same.

Was it helpful?

Solution

Create a before update trigger if you want to modify the row being updated.

CREATE TRIGGER sprLog BEFORE UPDATE ON Users FOR WACH ROW
BEGIN
     IF OLD.wl < NEW.wl THEN
        IF NEW.wl = 3 THEN
           INSERT INTO blocked(uid, time) VALUE(NEW.ID, 15);
           SET NEW.state = 3;
        END IF;
     END IF;
END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top