2

Hi this is my first trigger function on pgAdmin3 postgresql, I tried and have given up, I want to add a trigger to populate the item_sold table if an item has been sold. Im doing an assessment for etrading site.

CREATE TRIGGER trPopulateItemSold()
AFTER UPDATE ON tradebay.item FOR EACH ROW    
RETURN trigger AS $populate$
BEGIN
INSERT INTO item_sold (transaction_id, quantity, buyer_id, item_id) VALUES (transaction_id, NEW.quantity, buyer_id, NEW.item_id);
END;
LANGUAGE plpgsql;

SQL error:

ERROR:  syntax error at or near "("
LINE 1: CREATE TRIGGER trPopulateItemSold()
                                     ^

1 Answer 1

3

Your CREATE TRIGGER syntax is invalid. You're misunderstanding how it works.

Trigger creation is done in two steps:

  • Create the function that implements the trigger logic; then
  • Create a trigger that runs the function when something happens

See the PostgreSQL manual for details and examples:

You seem to be trying to mash the two together into a single step. That will not work. You need:

CREATE OR REPLACE FUNCTION .... RETURNS trigger AS $$
$$;

CREATE TRIGGER ...;
Sign up to request clarification or add additional context in comments.

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.