Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 14CE52E0214 for ; Wed, 12 Dec 2007 02:25:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 87483-10 for ; Wed, 12 Dec 2007 02:25:31 -0400 (AST) Received: from smtp136.iad.emailsrvr.com (smtp136.iad.emailsrvr.com [207.97.245.136]) by postgresql.org (Postfix) with ESMTP id BB8932E010B for ; Wed, 12 Dec 2007 02:25:33 -0400 (AST) Received: from relay3.r3.iad.emailsrvr.com (localhost [127.0.0.1]) by relay3.r3.iad.emailsrvr.com (SMTP Server) with ESMTP id 10881458A0A; Wed, 12 Dec 2007 01:25:33 -0500 (EST) Received: by relay3.r3.iad.emailsrvr.com (Authenticated sender: erik-AT-myemma.com) with ESMTP id E2F5C4589FA; Wed, 12 Dec 2007 01:25:32 -0500 (EST) In-Reply-To: <475F6E69.3060207@reynolds.com.au> References: <475F6E69.3060207@reynolds.com.au> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed Message-Id: Cc: pgsql-sql Content-Transfer-Encoding: quoted-printable From: Erik Jones Subject: Re: Function result using execute Date: Wed, 12 Dec 2007 00:25:10 -0600 To: Paul Lambert X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/63 X-Sequence-Number: 29755 On Dec 11, 2007, at 11:15 PM, Paul Lambert wrote: > I have a function which uses execute to populate the value of a =20 > variable based on a defined select construct. > > The relevant part of the code looks like thus: > EXECUTE curr_query INTO curr_amount; > RAISE NOTICE '%',curr_amount; > IF NOT FOUND THEN > curr_amount=3D0; > END IF; > RAISE NOTICE '%',curr_amount; > > I've added the if found to trap if nothing is returned by the =20 > execute so that the value gets set to a default 0 rather than null. > > When I call the function, the first raise notice gives me a value =20 > that is correct based on the select it would be performing, but the =20= > second raise notice gives me a 0, which suggests to me that =20 > although the execute has populated the curr_amount field with =20 > something, the IF NOT FOUND is always firing. > > Am I misunderstanding what the FOUND variable can be used for - =20 > i.e. is it not compatible with/not set by the EXECUTE command and =20 > should therefore I just be using a test of IF curr_amount IS NOT NULL? If the result of your execute doesn't assign any value(s) to =20 curr_amount it sets it to NULL. With that in mind, IF curr_amount IS NULL THEN curr_amount :=3D 0; END IF; should do. Erik Jones Software Developer | Emma=AE erik@myemma.com 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com