Thread: Update and trigger

Update and trigger

From
"Medi Montaseri"
Date:
Hi,<br /><br />I need to increament a counter such as myTable.Counter of type integer everytime myTable.status a
booleancolumn is updated. Can you help me complete this...<br /><br />create trigger counter_trigger after update on
myTable.counter<br /> execute procedure 'BEGIN statement; statement; statement END'<br /><br />Q1- how do I narrow the
eventto "update on a column not a row"<br />Q2- can I just provide an inline procedure for the execute<br /><br
/>Thanks<br/> Medi<br /> 

Re: Update and trigger

From
"A. Kretschmer"
Date:
am  Tue, dem 10.06.2008, um 18:45:51 -0700 mailte Medi Montaseri folgendes:
> Hi,
> 
> I need to increament a counter such as myTable.Counter of type integer
> everytime myTable.status a boolean column is updated. Can you help me complete
> this...
> 
> create trigger counter_trigger after update on myTable.counter
> execute procedure 'BEGIN statement; statement; statement END'

much simpler, use a RULE instead a TRIGGER like my example:

Suppose, i have a table called foo, it contains now:

test=# select * from foo;i
---12
(2 rows)


I create a sequence and a RULE:

test=*# create sequence foo_counter;
CREATE SEQUENCE
test=*# create or replace rule foo_update as on update to foo do also select nextval('foo_counter');
CREATE RULE


And now i do a update on foo:


test=*# update foo set i=2;nextval
---------      1
(1 row)

test=*# update foo set i=3;nextval
---------      2
(1 row)


test=*# select currval('foo_counter');currval
---------      2
(1 row)



Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net


Re: Update and trigger

From
Craig Ringer
Date:
Medi Montaseri wrote:
> Hi,
> 
> I need to increament a counter such as myTable.Counter of type integer 
> everytime myTable.status a boolean column is updated. Can you help me 
> complete this...
> 
> create trigger counter_trigger after update on myTable.counter
> execute procedure 'BEGIN statement; statement; statement END'
> 
> Q1- how do I narrow the event to "update on a column not a row"

Use a row-level trigger and test to see if the column of interest has 
been altered. Eg:

IF new.fieldname IS DISTINCT FROM old.fieldname THEN   -- Do the work
END IF;

> Q2- can I just provide an inline procedure for the execute

No, at present you must create a function that returns TRIGGER and then 
use that as the target to execute. At least as far as I know.

--
Craig Ringer


Re: Update and trigger

From
"Medi Montaseri"
Date:
Thanks...but a difference seems to be that the rule is not specific to update on a particular col but any col of a row
gettingupdated...<br /><br />Thanks<br /><br /><div class="gmail_quote">On Tue, Jun 10, 2008 at 10:21 PM, A. Kretschmer
<<ahref="mailto:andreas.kretschmer@schollglas.com">andreas.kretschmer@schollglas.com</a>> wrote:<br /><blockquote
class="gmail_quote"style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">am
 Tue,dem 10.06.2008, um 18:45:51 -0700 mailte Medi Montaseri folgendes:<br /><div class="Ih2E3d">> Hi,<br /> ><br
/>> I need to increament a counter such as myTable.Counter of type integer<br /> > everytime myTable.status a
booleancolumn is updated. Can you help me complete<br /> > this...<br /> ><br /> > create trigger
counter_triggerafter update on myTable.counter<br /> > execute procedure 'BEGIN statement; statement; statement
END'<br/><br /></div>much simpler, use a RULE instead a TRIGGER like my example:<br /><br /> Suppose, i have a table
calledfoo, it contains now:<br /><br /> test=# select * from foo;<br />  i<br /> ---<br />  1<br />  2<br /> (2
rows)<br/><br /><br /> I create a sequence and a RULE:<br /><br /> test=*# create sequence foo_counter;<br /> CREATE
SEQUENCE<br/> test=*# create or replace rule foo_update as on update to foo do also select nextval('foo_counter');<br
/>CREATE RULE<br /><br /><br /> And now i do a update on foo:<br /><br /><br /> test=*# update foo set i=2;<br />
 nextval<br/> ---------<br />       1<br /> (1 row)<br /><br /> test=*# update foo set i=3;<br />  nextval<br />
---------<br/>       2<br /> (1 row)<br /><br /><br /> test=*# select currval('foo_counter');<br />  currval<br />
---------<br/>       2<br /> (1 row)<br /><br /><br /><br /> Andreas<br /> --<br /> Andreas Kretschmer<br /> Kontakt:
 Heynitz:035242/47150,   D1: 0160/7141639 (mehr: -> Header)<br /> GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   <a
href="http://wwwkeys.de.pgp.net"target="_blank">http://wwwkeys.de.pgp.net</a><br /><font color="#888888"><br /> --<br
/>Sent via pgsql-sql mailing list (<a href="mailto:pgsql-sql@postgresql.org">pgsql-sql@postgresql.org</a>)<br /> To
makechanges to your subscription:<br /><a href="http://www.postgresql.org/mailpref/pgsql-sql"
target="_blank">http://www.postgresql.org/mailpref/pgsql-sql</a><br/></font></blockquote></div><br /> 

Re: Update and trigger

From
"A. Kretschmer"
Date:
am  Wed, dem 11.06.2008, um  0:54:55 -0700 mailte Medi Montaseri folgendes:
> Thanks...but a difference seems to be that the rule is not specific to update
> on a particular col but any col of a row getting updated...
> 
> Thanks

Okay, but i havn't an idea. A statement level trigger can't see the the
data in NEW and OLD. And a row level trigger fires for every row.


> 
> On Tue, Jun 10, 2008 at 10:21 PM, A. Kretschmer <
> andreas.kretschmer@schollglas.com> wrote:

Please, no silly top posting with fullquote below, i'm reading from top
to bottom.


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net