Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 2D71A6324C2 for ; Thu, 18 Feb 2010 05:34:08 -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 22433-06 for ; Thu, 18 Feb 2010 09:33:57 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f177.google.com (mail-qy0-f177.google.com [209.85.221.177]) by mail.postgresql.org (Postfix) with ESMTP id 8EA3F63232C for ; Thu, 18 Feb 2010 05:33:57 -0400 (AST) Received: by qyk7 with SMTP id 7so7499445qyk.10 for ; Thu, 18 Feb 2010 01:33:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:cc:content-type; bh=AAhEhbTWuE48YtHMvs1JdB5uKN3mg3GOj1+IST1o66A=; b=XXLyM8TowymlJc4GsSmnxm7kH+t0DI0XcvGqNt59RWIjsBRPtdI0s9zItgJnqYrwcF d9fnC3KG3pKwwg1XAt5BT81S8/oatj1K7jlo3Od3p+vCpOLfOc7Xz3AlQmEDxKJYqAGs 6egaEfXHNio+rYjHAHu6Rxe4kbkPUaHnA672A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=EgdlUqKpk0C0YCn6Bf3OavBtFlF5BhjrsY11QRzry5hpJMZgb5mP0NrG9NHfunzJeu NrlptY9LdzNYQk/ZcffVm84cWG9Vph183I35PzWfz106oNUma99hw0BkdX1nf6o6WQb1 M/JVyudh6oEfGjq9ZBWYX/gdnIZU65Y3Sn9V0= MIME-Version: 1.0 Received: by 10.229.88.197 with SMTP id b5mr2131695qcm.102.1266485636210; Thu, 18 Feb 2010 01:33:56 -0800 (PST) In-Reply-To: <4B7D0704.70301@archonet.com> References: <84b68b3d1002170718s5da61fam69de31ad1e54595f@mail.gmail.com> <4B7D0704.70301@archonet.com> From: Amitabh Kant Date: Thu, 18 Feb 2010 15:03:36 +0530 Message-ID: <84b68b3d1002180133x549f2b07p6d40682cd94d1d86@mail.gmail.com> Subject: Re: Referencing external table in update/insert triggers To: Richard Huxton Cc: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary=001636426e0f75a1c2047fdcaa7d X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201002/70 X-Sequence-Number: 33669 --001636426e0f75a1c2047fdcaa7d Content-Type: text/plain; charset=ISO-8859-1 On Thu, Feb 18, 2010 at 2:53 PM, Richard Huxton wrote: > 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 > Thanks Richard. With regards Amitabh Kant --001636426e0f75a1c2047fdcaa7d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Thu, Feb 18, 2010 at 2:53 PM, Richard= Huxton <dev@archo= net.com> wrote:
On 17/02/10 15:18, Amitabh Kant wrote:

CREATE OR REPLACE FUNCTION update_data() RETURNS TRIGGER AS $update_data$ BEGIN
=A0 =A0 IF NEW.t1f4> =A0t2.t2f4
=A0 =A0 =A0 =A0 UPDATE t2 set t2f2=3DNEW.t1f2, t2f3=3DNEW.t1f3, t2f4=3DNEW= .t1f4 where
t2f1=3DNEW.d1;
=A0 =A0 =A0 =A0 RETURN NEW;
=A0 =A0 END IF;
END;
$update_data$ LANGUAGE plpgsql;

I would like to compare the date present in the t2f4 with the new data bein= g
updated through the trigger. =A0Using the if line as listed above returns a= n
error.

You can always do something like:
=A0SELECT t2.t2f4 INTO my_variable FROM t2 WHERE ...
=A0IF NEW.t1f4 > my_variable

However, for this case you can just do an update with an extended where cla= use:
=A0UPDATE t2 ... WHERE t2f1=3DNEW.d1 AND NEW.t1f4 > t2.t2f4

No need for the IF.

--
=A0Richard Huxton
=A0Archonet Ltd


Thanks Richard.

With regards
<= br>Amitabh Kant
--001636426e0f75a1c2047fdcaa7d--