Testing for null record in plpgsql - Mailing list pgsql-sql

From Erik Jones
Subject Testing for null record in plpgsql
Date
Msg-id 491AB2F3-6ACF-4D58-A195-9D7A848E479A@myemma.com
Whole thread Raw
Responses Re: Testing for null record in plpgsql  (Craig Ringer <craig@postnewspapers.com.au>)
Re: Testing for null record in plpgsql  ("Pavel Stehule" <pavel.stehule@gmail.com>)
List pgsql-sql
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 $$
DECLAREres integer;
BEGINSELECT INTO res extract('month' from now()::integer % 2;IF res == 0 THEN    RETURN NULL;ELSE    RETURN
(5,5)::foo;ENDIF; 
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 $$
DECLAREres boolean;null_rec foo;
BEGINSELECT 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®
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





pgsql-sql by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [DOCS] [HACKERS] pl/PgSQL, variable names in NEW
Next
From: Craig Ringer
Date:
Subject: Re: Testing for null record in plpgsql