1

This answer says that "you should never call stored procedures from within triggers", however, it seems to be assuming some circumstances and needs that might not be applicable to every database. What if I don't need to roll data back? What if my data set is fairly small?

Specifically, I'm building a database and application, part of which will deal with inventory management, and I've made a stored procedure as follows:

drop procedure if exists quantityupdate;
DELIMITER //
create PROCEDURE quantityupdate()
begin
    DECLARE idcounter INT;
    DECLARE newq FLOAT;
    DECLARE partcount FLOAT;
    DECLARE used FLOAT;
    DECLARE added FLOAT;
    set idcounter = 1;
    set partcount = (SELECT COUNT(*) FROM parts);
    while idcounter < partcount DO
        SET used = (SELECT sum(Quantity) FROM `partusage` WHERE partID = idcounter AND SoftDelete = 0);
        SET added = (SELECT sum(Quantity) FROM `purchases` WHERE partID = idcounter AND SoftDelete = 0);
        SET newq = added - used;
        UPDATE `parts` SET `QuantityOnHand` = if (newq IS NOT NULL, (SELECT newq),QuantityOnHand) WHERE partID = idcounter;
        set idcounter = idcounter + 1;
    end while;
end //
delimiter ;

I'm considering calling this stored procedure whenever a record is INSERTed into the purchases and partusage tables, which will happen only 50 times per day or so. Is there a reason that I shouldn't call that from a trigger like that?

1
  • In my opinion, you are depending too much on Triggers and Stored Routines. As your question implies, you are stepping into complex and somewhat uncharted areas. I recommend you back some of the code into your application.
    – Rick James
    Commented Apr 7, 2017 at 19:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.