Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id E92BB2E004F for ; Fri, 11 Apr 2008 01:16:50 -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 10531-03 for ; Fri, 11 Apr 2008 01:16:48 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtp176.iad.emailsrvr.com (smtp176.iad.emailsrvr.com [207.97.245.176]) by developer.postgresql.org (Postfix) with ESMTP id 54F152E003D for ; Fri, 11 Apr 2008 01:16:48 -0300 (ADT) Received: from relay7.relay.iad.mlsrvr.com (localhost [127.0.0.1]) by relay7.relay.iad.mlsrvr.com (SMTP Server) with ESMTP id 398031B40D7 for ; Fri, 11 Apr 2008 00:16:47 -0400 (EDT) Received: by relay7.relay.iad.mlsrvr.com (Authenticated sender: erik-AT-myemma.com) with ESMTP id 22F441B40C5 for ; Fri, 11 Apr 2008 00:16:47 -0400 (EDT) Message-Id: <491AB2F3-6ACF-4D58-A195-9D7A848E479A@myemma.com> From: Erik Jones To: "pgsql-sql@postgresql.org List" Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v919.2) Subject: Testing for null record in plpgsql Date: Thu, 10 Apr 2008 23:16:40 -0500 X-Mailer: Apple Mail (2.919.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/72 X-Sequence-Number: 30611 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; =09 IF res =3D=3D 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 =20= result to see if I have a null record (null, null),. I've got the =20 following working but it feels like there should be something much =20 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=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