PL/pgSQL triggers ON INSERT OR UPDATE - Mailing list pgsql-sql

From san@cobalt.rmnet.it
Subject PL/pgSQL triggers ON INSERT OR UPDATE
Date
Msg-id 200110222125.XAA30632@cobalt.rmnet.it
Whole thread Raw
Responses Re: PL/pgSQL triggers ON INSERT OR UPDATE  ("Aasmund Midttun Godal" <postgresql@envisity.com>)
List pgsql-sql
Hello, I'm trying to set up a trigger on insert or update
but when using the predefined variable ``OLD'' I get a
NOTICE from the trigger function about OLD not being defined yet.

Of course OLD is not defined when the function is triggered on INSERT
event, and I did not mention it if not inside a conditional block
checking for the TG_OP variable being 'UPDATE'.

For better understanding here is some code:

BEGIN
    IF TG_OP = 'UPDATE' THEN
        IF OLD.id <> NEW.id THEN
            -- do the work
        END IF
    END IF;
END;

Even when TG_OP != 'UPDATE' (INSERT event) I still get an error
message from the pl/pgsql compiler (the first time the trigger is fired).

What should I do then ? Is it still possible to use the same function
for UPDATE OR INSERT events ?

TIA

--san;

pgsql-sql by date:

Previous
From: "Pat M"
Date:
Subject: Re: Deleting obsolete values
Next
From: "Mayuresh Kadu"
Date:
Subject: Auto Increment