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
---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives?
http://archives.postgresql.org
