Обсуждение: Trigger function that works with both updates and deletes?

Поиск
Список
Период
Сортировка

Trigger function that works with both updates and deletes?

От
novnov
Дата:
Most of the trigger fuctions I've written work on new and updated records
referencing NEW. etc. I will need some of the trigger functions to work with
record deletions too.

First, when a record is being deleted, OLD refers to the rec just deleted
(or about to be deleted)?

Second, while I could write two trigger functions, one dealing with
add/update, the other with deletes, it's probably neater to have a single
trigger function and have it discriminate "am I being called for a delete,
or an add/update?" I don't know how to determine the type record change.
--
View this message in context:
http://www.nabble.com/Trigger-function-that-works-with-both-updates-and-deletes--tf3943732.html#a11186941
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Trigger function that works with both updates and deletes?

От
Michael Fuhr
Дата:
On Mon, Jun 18, 2007 at 06:07:37PM -0700, novnov wrote:
> First, when a record is being deleted, OLD refers to the rec just deleted
> (or about to be deleted)?

Correct.

> Second, while I could write two trigger functions, one dealing with
> add/update, the other with deletes, it's probably neater to have a single
> trigger function and have it discriminate "am I being called for a delete,
> or an add/update?" I don't know how to determine the type record change.

In PL/pgSQL you can use TG_OP.  See "Trigger Procedures" in the
documentation:

http://www.postgresql.org/docs/8.2/interactive/plpgsql-trigger.html

--
Michael Fuhr

Re: Trigger function that works with both updates and deletes?

От
"Harvey, Allan AC"
Дата:
> Second, while I could write two trigger functions, one dealing with
> add/update, the other with deletes, it's probably neater to 
> have a single
> trigger function and have it discriminate "am I being called 
> for a delete,
> or an add/update?" I don't know how to determine the type 
> record change.
I use trigger arguments and determine what to do by:

        if TG_ARGV[0] = 'value'
        then
            ....
        end if;

Allan


The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended
recipient,use, disclosure or copying of this information is prohibited. If you have received this document in error,
pleaseadvise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses
containedin this email or any attachments.
 

Re: Trigger function that works with both updates and deletes?

От
novnov
Дата:
Perfect, thanks


Michael Fuhr wrote:
>
> On Mon, Jun 18, 2007 at 06:07:37PM -0700, novnov wrote:
>> First, when a record is being deleted, OLD refers to the rec just deleted
>> (or about to be deleted)?
>
> Correct.
>
>> Second, while I could write two trigger functions, one dealing with
>> add/update, the other with deletes, it's probably neater to have a single
>> trigger function and have it discriminate "am I being called for a
>> delete,
>> or an add/update?" I don't know how to determine the type record change.
>
> In PL/pgSQL you can use TG_OP.  See "Trigger Procedures" in the
> documentation:
>
> http://www.postgresql.org/docs/8.2/interactive/plpgsql-trigger.html
>
> --
> Michael Fuhr
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match
>
>

--
View this message in context:
http://www.nabble.com/Trigger-function-that-works-with-both-updates-and-deletes--tf3943732.html#a11189206
Sent from the PostgreSQL - general mailing list archive at Nabble.com.