Re: Trigger.. AFTER and BEFORE with specific column changed

Lists: pgsql-general
From: Albert <oo_O2_oo(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Trigger.. AFTER and BEFORE with specific column changed
Date: 2012-04-01 18:45:39
Message-ID: 1333305939444-5610712.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

*I'm trying to follow this :*

CREATE TRIGGER check_update
*BEFORE* UPDATE ON accounts
FOR EACH ROW
WHEN (OLD.balance IS DISTINCT FROM NEW.balance)
EXECUTE PROCEDURE check_account_update();

*but with AFTER instead of BEFORE. and I'm keep getting error on or near
WHEN.
does that because i have to use BEFORE ? what if i need to execute the
procedure after updating column and it has really changed.*

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Trigger-AFTER-and-BEFORE-with-specific-column-changed-tp5610712p5610712.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Albert <oo_O2_oo(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger.. AFTER and BEFORE with specific column changed
Date: 2012-04-01 19:13:23
Message-ID: 4F78A8D3.8040504@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 04/01/2012 11:45 AM, Albert wrote:
> *I'm trying to follow this :*
>
>
> CREATE TRIGGER check_update
> *BEFORE* UPDATE ON accounts
> FOR EACH ROW
> WHEN (OLD.balance IS DISTINCT FROM NEW.balance)
> EXECUTE PROCEDURE check_account_update();
>
> *but with AFTER instead of BEFORE. and I'm keep getting error on or near
> WHEN.

What is the error message?

> does that because i have to use BEFORE ? what if i need to execute the
> procedure after updating column and it has really changed.*

Depends what the procedure is doing. For more detail on what the WHEN
does in BEFORE and AFTER triggers see here:
http://www.postgresql.org/docs/9.0/interactive/sql-createtrigger.html

Look for the Notes section.

>
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/Trigger-AFTER-and-BEFORE-with-specific-column-changed-tp5610712p5610712.html
> Sent from the PostgreSQL - general mailing list archive at Nabble.com.
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com


From: Albert <oo_O2_oo(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger.. AFTER and BEFORE with specific column changed
Date: 2012-04-02 05:10:08
Message-ID: 1333343408489-5611501.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

*i get:*

Syntax error at or near 'WHEN'
LINE 1: ... check_update AFTER UPDATE ON accounts FOR EACH ROW WHEN
(OLD.balance IS DISTINCT FROM NEW.balance) ....

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Trigger-AFTER-and-BEFORE-with-specific-column-changed-tp5610712p5611501.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Albert <oo_O2_oo(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger.. AFTER and BEFORE with specific column changed
Date: 2012-04-02 13:30:43
Message-ID: 4F79AA03.9050501@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 04/01/2012 10:10 PM, Albert wrote:
> *i get:*
>
> Syntax error at or near 'WHEN'
> LINE 1: ... check_update AFTER UPDATE ON accounts FOR EACH ROW WHEN
> (OLD.balance IS DISTINCT FROM NEW.balance) ....

Hmmm. So two questions:

What happens if you copy the example in the docs exactly and use NEW.*
and OLD.*?

What is the table definition for accounts?

>
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/Trigger-AFTER-and-BEFORE-with-specific-column-changed-tp5610712p5611501.html
> Sent from the PostgreSQL - general mailing list archive at Nabble.com.
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Albert <oo_O2_oo(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger.. AFTER and BEFORE with specific column changed
Date: 2012-04-02 13:53:50
Message-ID: 1760.1333374830@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Albert <oo_O2_oo(at)hotmail(dot)com> writes:
> *i get:*
> Syntax error at or near 'WHEN'
> LINE 1: ... check_update AFTER UPDATE ON accounts FOR EACH ROW WHEN
> (OLD.balance IS DISTINCT FROM NEW.balance) ....

Does the BEFORE case work for you either? I'm suspicious that you
are using a PG version that predates WHEN-clause support in triggers.

regards, tom lane