0

I created trigger and function.

CREATE TRIGGER trigger_update_quantity AFTER INSERT ON orderitem
    FOR EACH ROW EXECUTE PROCEDURE update_quantity();

        CREATE OR REPLACE FUNCTION update_quantity() RETURNS TRIGGER AS $quantity_update$
          DECLARE
            flower INT;
          BEGIN
            flower = New.flower_num;
            UPDATE product SET quantity = quantity - New.quantity
            WHERE flower_num = flower;
          END;
          $quantity_update$
        LANGUAGE plpgsql;

When I try to insert a row into a table. Everything i have the following error.

INSERT INTO "public"."orderitem" ("order_num", "flower_num", "quantity", "total_price") VALUES (?, ?, ?, ?);

            [2F005] ERROR: control reached end of trigger procedure without RETURN
            Where: PL/pgSQL function update_quantity()
3
  • I might be mistaken but I think a trigger function should return values he mlust use for his action (here INSERT). So in your example, I would return the NEW value. Commented Jul 27, 2017 at 11:50
  • I cannot return NEW. Give an example of what to return Commented Jul 27, 2017 at 12:14
  • You will find an example in the documentation Commented Jul 27, 2017 at 12:30

2 Answers 2

2

Here's what I would write (not tested):

    CREATE OR REPLACE FUNCTION update_quantity() RETURNS TRIGGER AS $quantity_update$
      DECLARE
        flower INT;
      BEGIN
        flower = New.flower_num;
        UPDATE product SET quantity = quantity - New.quantity
        WHERE flower_num = flower;
        RETURN NEW;
      END;
      $quantity_update$
    LANGUAGE plpgsql;

CREATE TRIGGER trigger_update_quantity AFTER INSERT ON orderitem
    FOR EACH ROW EXECUTE PROCEDURE update_quantity();
Sign up to request clarification or add additional context in comments.

Comments

1

how is AFTER TRIGGER, you can return RETURN OLD; or RETURN NULL;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.