pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to execute my trigger when update certain columns but not all?
2+ messages / 2 participants
[nested] [flat]

* How to execute my trigger when update certain columns but not all?
@ 2002-08-14 12:09  Raymond Chui <Raymond.Chui@noaa.gov>
  0 siblings, 1 reply; 2+ messages in thread

From: Raymond Chui @ 2002-08-14 12:09 UTC (permalink / raw)
  To: pgsql-sql; pgadmin-support@postgresql.org; pgsql-general@postgresql.org; pgsql-admin@postgresql.org

Let say I have a table has column1 and column2 and I made a trigger for
after INSERT OR UPDATE for each row to execute procedure my_function.

What I want is the trigger execute my_function only when column1 is
insert or
update, but not going to execute my_function when column2 is insert or
update.
How do I do that?

Thank Q very much

Attachments:

raymond.chui.vcf (text/x-vcard, 311B) download begin:vcard n:Chui;Raymond tel;work:301-713-0640 x168 x-mozilla-html:FALSE url:http://www.nws.noaa.gov/ org:NWS, NOAA;OHD13 version:2.1 email;internet:raymond.chui@noaa.gov title:CS adr;quoted-printable:;;1325 East-West Highway=0D=0ASSMC2, Room 8112;Silver Spring;MD;20910-3282;U.S.A. fn:Raymond Chui end:vcard
^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: [ADMIN] How to execute my trigger when update certain columns
@ 2002-08-22 06:10  Oliver Elphick <olly@lfix.co.uk>
  parent: Raymond Chui <Raymond.Chui@noaa.gov>
  0 siblings, 0 replies; 2+ messages in thread

From: Oliver Elphick @ 2002-08-22 06:10 UTC (permalink / raw)
  To: Raymond Chui <Raymond.Chui@noaa.gov>; +Cc: pgsql-sql; pgadmin-support@postgresql.org; pgsql-general@postgresql.org; pgsql-admin@postgresql.org

On Wed, 2002-08-14 at 13:09, Raymond Chui wrote:
> Let say I have a table has column1 and column2 and I made a trigger for
> after INSERT OR UPDATE for each row to execute procedure my_function.
> 
> What I want is the trigger execute my_function only when column1 is
> insert or
> update, but not going to execute my_function when column2 is insert or
> update.

The trigger is executed unconditionally, so put the condition inside
my_function.

If it's an INSERT, column1 must be new, so:

IF TG_OP = ''INSERT'' OR
  (TG_OP = ''UPDATE'' AND
     (NEW.column1 != OLD.column1 OR
       (NEW.column1 IS NULL AND OLD.column1 IS NOT NULL) OR 
       (NEW.column1 IS NOT NULL AND OLD.column1 IS NULL)
     )
  ) THEN
...
END IF;

-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                            
http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "For yourselves know perfectly that the day of the Lord
      so cometh as a thief in the night. For when they shall
      say, Peace and safety; then sudden destruction cometh 
      upon them, as travail upon a woman with child; and 
      they shall not escape."      I Thessalonians 5:2,3 




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-08-22 06:10 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-08-14 12:09 How to execute my trigger when update certain columns but not all? Raymond Chui <Raymond.Chui@noaa.gov>
2002-08-22 06:10 ` Re: [ADMIN] How to execute my trigger when update certain columns Oliver Elphick <olly@lfix.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox