Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 94727632903 for ; Thu, 18 Feb 2010 05:23:30 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 20696-10 for ; Thu, 18 Feb 2010 09:23:19 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.ptn-ipout02.plus.net (relay.ptn-ipout02.plus.net [212.159.7.36]) by mail.postgresql.org (Postfix) with ESMTP id 04E3E63284D for ; Thu, 18 Feb 2010 05:23:19 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvsEADuWfEvUnw4R/2dsb2JhbACbB3S9eoRmBA Received: from pih-relay04.plus.net ([212.159.14.17]) by relay.ptn-ipout02.plus.net with ESMTP; 18 Feb 2010 09:23:17 +0000 Received: from [84.51.143.99] (helo=server3.office.archonet.com) by pih-relay04.plus.net with esmtp (Exim) id 1Ni2bN-0003Aa-AE; Thu, 18 Feb 2010 09:23:17 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTPA id EDDF2274032; Thu, 18 Feb 2010 09:23:16 +0000 (GMT) Message-ID: <4B7D0704.70301@archonet.com> Date: Thu, 18 Feb 2010 09:23:16 +0000 From: Richard Huxton User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9pre) Gecko/20100213 Lightning/1.0b1 Shredder/3.0.2pre MIME-Version: 1.0 To: Amitabh Kant CC: pgsql-sql@postgresql.org Subject: Re: Referencing external table in update/insert triggers References: <84b68b3d1002170718s5da61fam69de31ad1e54595f@mail.gmail.com> In-Reply-To: <84b68b3d1002170718s5da61fam69de31ad1e54595f@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Plusnet-Relay: c09343d35fe944d29632904001550cf2 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 201002/69 X-Sequence-Number: 33668 On 17/02/10 15:18, Amitabh Kant wrote: > > CREATE OR REPLACE FUNCTION update_data() RETURNS TRIGGER AS $update_data$ > BEGIN > IF NEW.t1f4> t2.t2f4 > UPDATE t2 set t2f2=NEW.t1f2, t2f3=NEW.t1f3, t2f4=NEW.t1f4 where > t2f1=NEW.d1; > RETURN NEW; > END IF; > END; > $update_data$ LANGUAGE plpgsql; > I would like to compare the date present in the t2f4 with the new data being > updated through the trigger. Using the if line as listed above returns an > error. You can always do something like: SELECT t2.t2f4 INTO my_variable FROM t2 WHERE ... IF NEW.t1f4 > my_variable However, for this case you can just do an update with an extended where clause: UPDATE t2 ... WHERE t2f1=NEW.d1 AND NEW.t1f4 > t2.t2f4 No need for the IF. -- Richard Huxton Archonet Ltd