Home > database >  Creating a Trigger to change negative input values to positive values before insert
Creating a Trigger to change negative input values to positive values before insert

Time:10-19

I need to create a Trigger that checks BEFORE INSERT. For each row, if pc.price <= 0 then the price should be set to its absolute value

CREATE TRIGGER Neg_PC_Price
BEFORE INSERT ON PC
FOR EACH ROW
BEGIN
    IF NEW.price <= 0
    THEN ABS(NEW.price)
    END IF;
END;

This is what I pieced together from a different solution but instead of setting the new price they returned an error statement and not sure how to input with the updated positive price.

CodePudding user response:

I do not see the reason in additional and obviously excess IF statement. Simply

CREATE TRIGGER Neg_PC_Price
BEFORE INSERT ON PC
FOR EACH ROW
SET NEW.price = ABS(NEW.price);
  • Related