Adam
Adam

Reputation: 181

TRIGGER not inserting values in table

I have the following trigger

CREATE TRIGGER Insert_Orders BEFORE INSERT ON `Orders`
FOR EACH ROW BEGIN
    INSERT INTO Payments (RestaurantPaymentInVAT, VATID) VALUES (
        (SELECT Get_RestaurantPaymentInVAT(NEW.`OrderID`)), 1);
END $$

However payments.RestaurantPaymentInVAT in the table results window doesn't show my any kind of non-null results. All of it is null. HOWEVER:

When I test it as a standalone query

SELECT Get_RestaurantPaymentInVAT(NEW.`OrderID`)

It gives the correct value.

What am I doing wrong?

Upvotes: 0

Views: 38

Answers (1)

Kevin Bott
Kevin Bott

Reputation: 733

I'm assuming that OrderID is defined as auto-increment. Since you're using a BEFORE INSERT trigger, OrderID is indeed NULL, because it has not been assigned a value yet. It will get a value AFTER it the record has been inserted. Try changing to an AFTER INSERT trigger, or explicitly providing a value for OrderID. That should fix it.

Upvotes: 1

Related Questions