mysql - Can I use SUM() inside of a SELECT INTO statement on a trigger? -
I want to sum every bill amount of the same foreign key. If the amount is less than zero, then I want to update an enum field in the FAQ table. I am not getting anything about using the amount () in any selection, but it would be great if this would work.
Create trigger after entry at `bill_log` for each 'bill_log_after_insert` ROW DECLARE @ Decimal figure; SELECT SUM (`bill_amount`) from @ bill_log at position @ where 'main_fk` = new.' Main_fk`; If the status of the & lt; 0 then set up 'main' transaction_type = 'credit' where `main_pk` = new. ' Mean_frack`; end if; Select
SUM using INTO Statement is valid but there are some other problems with your trigger: If you want to do a local variable DECLARE , then you can call it @ Can not prefix with sign as if the session variable is required for the local variable within the trigger and is not globally for the current session because it is for session variables This is a good practice to separate the local variable (with a v ) in a query from the unfiltered field names, because the error may be due to the same name or at least unexpected behavior . In addition, you need to specify the exact DECIMAL field or variable when you DECLARE default DCIML (10,0) means that the number can be 10 digits long and there is no decimal place. If you set it to DCIML (8, 2) , the number can be 8 digits long, but if two of them are in decimal places DELIMITER / / create bill_log_after_insert trigger after INSERT for everyday / / / / /> SELECT SUM (bill_mount) in v_amount to bill_log WHERE main_fk = NEW.main_fk; If v_amount & lt; 0 then update the main SET transaction_type = 'credit' WHERE main_pk = NEW.main_fk; end if; END // DELIMITER; Demo @
Comments
Post a Comment