0

I've written a trigger like

CREATE TRIGGER myTrigger
INSTEAD OF INSERT ON myTable
EXECUTE PROCEDURE myFunction(); 

but I've learned that the INSTEAD OF trigger cannot be applied to a table.

The only way to do it should be by using

BEFORE OF INSERT

the trigger should retrieve some values (unknown when the user calls the insert function) on the other table and insert this value inside firing query and execute it. How can I do this?

1
  • 1
    If you're going to post multiple questions in rapid succession please link back to your earlier questions. You should also always mention your PostgreSQL version in your questions. Commented Aug 11, 2013 at 10:00

1 Answer 1

1

You would write a PL/PgSQL trigger that:

  • Checks tg_op to determine whether the operation is an INSERT, UPDATE or DELETE; and

  • Based on the operation type, uses the NEW and/or OLD pseudo-variables to construct and run INSERTs, UPDATEs, or DELETEs on the real target tables; then

  • Return NULL so that no action is taken on the original target table.

See the basic documentation on PL/PgSQL triggers.

This is a pretty weird thing to want to do unless you're using table inheritance, though.

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.