Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 939222E0060 for ; Fri, 11 Apr 2008 10:01:19 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 44827-04 for ; Fri, 11 Apr 2008 10:01:06 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.158]) by developer.postgresql.org (Postfix) with ESMTP id BFBA12E002C for ; Fri, 11 Apr 2008 10:01:06 -0300 (ADT) Received: by fg-out-1718.google.com with SMTP id 22so389131fge.23 for ; Fri, 11 Apr 2008 06:01:04 -0700 (PDT) Received: by 10.86.61.13 with SMTP id j13mr4804831fga.26.1207891319005; Thu, 10 Apr 2008 22:21:59 -0700 (PDT) Received: by 10.86.80.3 with HTTP; Thu, 10 Apr 2008 22:21:58 -0700 (PDT) Message-ID: <162867790804102221w5cb2ca8uf9faad037f1a789e@mail.gmail.com> Date: Fri, 11 Apr 2008 07:21:58 +0200 From: "Pavel Stehule" To: "Erik Jones" Subject: Re: Testing for null record in plpgsql Cc: "pgsql-sql@postgresql.org List" In-Reply-To: <491AB2F3-6ACF-4D58-A195-9D7A848E479A@myemma.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <491AB2F3-6ACF-4D58-A195-9D7A848E479A@myemma.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/74 X-Sequence-Number: 30613 Hello maybe CREATE OR REPLACE FUNCTION returns_empty_set_maybe() RETURNS SETOF foo AS $$ res integer; aux foo; BEGIN SELECT INTO res extract('month' from now()::integer % 2; IF res <> 0 THEN aux = (5,5); RETURN NEXT aux; END IF; RETURN; END; $$ LANGUAGE plpgsql; and then in your function DECLARE x foo; BEGIN SELECT INTO x * FROM returns_empty_set_maybe(); RETURN found; END; Regards Pavel Stehule On 11/04/2008, Erik Jones wrote: > Ok, let's say I have the following: > > CREATE TABLE foo ( > val1 integer, > val2 integer > ); > > CREATE OR REPLACE FUNCTION returns_null_maybe() > RETURNS foo AS $$ > DECLARE > res integer; > BEGIN > SELECT INTO res extract('month' from now()::integer % 2; > > IF res == 0 THEN > RETURN NULL; > ELSE > RETURN (5,5)::foo; > END IF; > END; > $$ LANGUAGE plpgsql; > > Now, let's say I want to call this from another function and test the > result to see if I have a null record (null, null),. I've got the following > working but it feels like there should be something much simpler but I just > can't seem to hit on it. Is this it? > > CREATE FUNCTION test_null_rec() > RETURNS boolean AS $$ > DECLARE > res boolean; > null_rec foo; > BEGIN > SELECT INTO res * FROM returns_null_maybe(); > > IF row(res.*) IS DISTINCT FROM row(null_rec.*) THEN > RETURN TRUE; > ELSE > RETURN FALSE; > END IF; > END; > $$ LANGUAGE plpgsql; > > Erik Jones > > DBA | Emma(R) > 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 > > > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >