From: Tomasz Myrta <jasiek@klaster.net>
To: Jeff Kowalczyk <jtk@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Trigger plpgsql function, how to test if OLD is set?
Date: Thu, 04 Dec 2003 19:36:58 +0100
Message-ID: <3FCF7ECA.5060200@klaster.net> (raw)
In-Reply-To: <pan.2003.12.04.18.17.53.593118@yahoo.com>
References: <pan.2003.12.04.18.17.53.593118@yahoo.com>
Dnia 2003-12-04 19:18, Użytkownik Jeff Kowalczyk napisał:
> When this trigger runs on INSERT operations, the OLD variable is not
> yet set, and the trigger function returns an error.
>
> Can anyone suggest a more sensible way to check for OLD before including
> it in my expression, or another shortcut? Thanks.
>
> CREATE OR REPLACE FUNCTION orders_initordercharges () RETURNS "trigger"
> AS '
> BEGIN
> -- Check that no ordercharges exist for this orderid
> IF (SELECT COUNT(orderchargeid) FROM ordercharges WHERE orderid=NEW.orderid OR orderid=OLD.orderid)=0 THEN
> -- Insert standard initial set of ordercharges
> INSERT INTO ordercharges (orderid, orderchargecode) VALUES (NEW.orderid,\'SALE\');
> INSERT INTO ordercharges (orderid, orderchargecode) VALUES (NEW.orderid,\'S&H\');
> END IF;
> RETURN NEW;
> END;
> '
> LANGUAGE plpgsql;
You can always check whether your trigger has been fired as insert or
update trigger.
DECLARE
old_orderid integer;
BEGIN
if TG_OP=''UPDATE'' then
old_orderid=OLD.orderid;
else
old_orderid=-1;
end if;
...
Regards,
Tomasz Myrta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jasiek@klaster.net, jtk@yahoo.com
Subject: Re: Trigger plpgsql function, how to test if OLD is set?
In-Reply-To: <3FCF7ECA.5060200@klaster.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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