Re: Providing an alternative result when there is no result

From: David Fetter <david(at)fetter(dot)org>
To: Joshua Berry <yoberi(at)gmail(dot)com>
Cc: Postgresql General Mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Providing an alternative result when there is no result
Date: 2009-05-19 13:08:11
Message-ID: 20090519130811.GC7537@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, May 18, 2009 at 03:13:56PM -0400, Joshua Berry wrote:
> Hello all,
>
> Is there an easy and efficient way to return a boolean false for a query
> that returns no result, and true for one that does return a result?
>
> Currently we select the result into a temp table.
>
> SELECT INTO temp_table id FROM ... ;
> IF temp_table IS NULL THEN
> resp:= 'NO';
> ELSE
> resp:= 'YES';
> END IF;

SELECT EXISTS (SELECT 1 FROM ....);

will get you a boolean which can't be NULL. You can either map that
to "yes/no" or return it as is.

Hope this helps :)

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Guettler 2009-05-19 13:32:16 Re: Get block of N numbers from sequence
Previous Message Stephane Bortzmeyer 2009-05-19 13:05:21 Restoring a database from original files (Was: Need help