X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id F06F8D1B474 for ; Thu, 4 Dec 2003 18:18:43 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 86836-06 for ; Thu, 4 Dec 2003 14:18:13 -0400 (AST) Received: from main.gmane.org (main.gmane.org [80.91.224.249]) by svr1.postgresql.org (Postfix) with ESMTP id 856F4D1B444 for ; Thu, 4 Dec 2003 14:18:11 -0400 (AST) Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1ARy3A-0001qp-00 for ; Thu, 04 Dec 2003 19:18:04 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org Received: from sea.gmane.org ([80.91.224.252]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1ARy39-0001qh-00 for ; Thu, 04 Dec 2003 19:18:03 +0100 Received: from news by sea.gmane.org with local (Exim 3.35 #1 (Debian)) id 1ARy39-000666-00 for ; Thu, 04 Dec 2003 19:18:03 +0100 From: Jeff Kowalczyk Subject: Trigger plpgsql function, how to test if OLD is set? Date: Thu, 04 Dec 2003 13:18:02 -0500 Lines: 42 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@sea.gmane.org User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=1.1 tagged_above=0.0 required=5.0 tests=BAYES_20, FORGED_YAHOO_RCVD, UPPERCASE_25_50, USER_AGENT X-Spam-Level: * X-Archive-Number: 200312/36 X-Sequence-Number: 16202 I have a test I need to do in my trigger function to see if a standard set of shipmentcharges exists, if not I insert two rows. IF (SELECT COUNT(orderchargeid) FROM ordercharges WHERE orderid=NEW.orderid OR orderid=OLD.orderid)=0 THEN I added the "OR orderid=OLD.orderid" expression to handle the case where the orderid is changed. A cascading update causes a duplicate set of shipmentcharges to be added for the shipmentid, since the expression is momentarily true. 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; CREATE TRIGGER orders_initordercharges BEFORE INSERT OR UPDATE ON orders FOR EACH ROW EXECUTE PROCEDURE orders_initordercharges (); ALTER TABLE ONLY ordercharges ADD CONSTRAINT if_order_exists FOREIGN KEY (orderid) REFERENCES orders(orderid) ON UPDATE CASCADE ON DELETE CASCADE;