Jump to content

Recommended Posts

basically I had something like this:

DELIMITER ~
CREATE TRIGGER updTrgr BEFORE INSERT ON tester
FOR EACH ROW BEGIN
    UPDATE tester SET tester.order = tester.order + 1 WHERE new.order <= tester.order;
END;~
DELIMITER ;

 

now this works (or the one that I used last time did)

 

but I get an error message on inserting.. it says something like..

 

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

 

so how do I do this? :S

Link to comment
https://forums.phpfreaks.com/topic/211506-triggers/
Share on other sites

I just posted the answer in other post  :D

 

"A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger."

 

http://dev.mysql.com/doc/refman/5.1/en/stored-program-restrictions.html

Link to comment
https://forums.phpfreaks.com/topic/211506-triggers/#findComment-1102725
Share on other sites

ok I Just read most of that article and that doesn't help me, how would I do this WITH A TRIGGER to achieve the same effect I am looking for :)

 

Thanks for the help though much appreciated :)

 

well... seems that you didn't read enough or you don't have a good understanding about how triggers work... which is ok.. we all started in some point :) (no offense intended)

 

lets see...

what you wrote was:

DELIMITER ~
CREATE TRIGGER updTrgr BEFORE INSERT ON tester
FOR EACH ROW BEGIN
    UPDATE tester SET tester.order = tester.order + 1 WHERE new.order <= tester.order;
END;~
DELIMITER ;

 

just look the code... and ask yourself

- why if what I wrote is a BEFORE INSERT trigger I'm trying to UPDATE a row that is not there? (you are just inserting that row)... this doesn't make sense.

 

seems that what you want to do is

DELIMITER ~
CREATE TRIGGER updTrgr BEFORE UPDATE ON tester
FOR EACH ROW BEGIN
    IF NEW.order <= OLD.order THEN
        SET NEW.order = OLD.order + 1;
    ENDIF;
END;~
DELIMITER ;

 

 

Link to comment
https://forums.phpfreaks.com/topic/211506-triggers/#findComment-1102763
Share on other sites

ok I Just read most of that article and that doesn't help me, how would I do this WITH A TRIGGER to achieve the same effect I am looking for :)

 

Thanks for the help though much appreciated :)

 

well... seems that you didn't read enough or you don't have a good understanding about how triggers work... which is ok.. we all started in some point :) (no offense intended)

 

lets see...

what you wrote was:

DELIMITER ~
CREATE TRIGGER updTrgr BEFORE INSERT ON tester
FOR EACH ROW BEGIN
    UPDATE tester SET tester.order = tester.order + 1 WHERE new.order <= tester.order;
END;~
DELIMITER ;

 

just look the code... and ask yourself

- why if what I wrote is a BEFORE INSERT trigger I'm trying to UPDATE a row that is not there? (you are just inserting that row)... this doesn't make sense.

 

seems that what you want to do is

DELIMITER ~
CREATE TRIGGER updTrgr BEFORE UPDATE ON tester
FOR EACH ROW BEGIN
    IF NEW.order <= OLD.order THEN
        SET NEW.order = OLD.order + 1;
    ENDIF;
END;~
DELIMITER ;

 

nooo you misunderstand man <3 I didn't read too far you're right but I'm not trying to do what you're doing :)

 

I'm trying to update ALL OTHER rows in the table?

 

for example..

 

order

1

2

3

4

5

 

I add a row with the order "2"

 

I want it to do this..

 

order

1

2

2 -> 3

3 -> 4

4 -> 5

5 -> 6

 

understand?

Link to comment
https://forums.phpfreaks.com/topic/211506-triggers/#findComment-1102795
Share on other sites

gotcha....  so.. my first answer was correct..... that is not possible to do that WITH A TRIGGER  for this reason:

 

"A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger."

Link to comment
https://forums.phpfreaks.com/topic/211506-triggers/#findComment-1102799
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.