Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Testing for nul...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 3403 of 3559
Post > Topic >>

Testing for null record in plpgsql

by erik@[EMAIL PROTECTED] (Erik Jones) Apr 10, 2008 at 11:16 PM

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=20
result to see if I have a null record (null, null),.  I've got the=20=20
following working but it feels like there should be something much=20=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@[EMAIL PROTECTED]
 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com




--=20
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 4 Posts in Topic:
Testing for null record in plpgsql
erik@[EMAIL PROTECTED] (  2008-04-10 23:16:40 
Re: Testing for null record in plpgsql
craig@[EMAIL PROTECTED]   2008-04-11 15:40:53 
Re: Testing for null record in plpgsql
erik@[EMAIL PROTECTED] (  2008-04-11 09:35:28 
Re: Testing for null record in plpgsql
pavel.stehule@[EMAIL PROT  2008-04-11 07:21:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Sun Jul 6 20:08:22 CDT 2008.