Re: Default value if query returns 0 rows?

From: Lars Kellogg-Stedman <lars(at)oddbit(dot)com>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Default value if query returns 0 rows?
Date: 2004-09-17 15:56:57
Message-ID: Pine.LNX.4.44.0409171130480.8060-100000@wolery.deas.harvard.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 17 Sep 2004, Bruno Wolff III wrote:

> >
> > if exists (select 1 from map where name = 'foo') then
> > select id from map where name = 'foo'
> > else
> > select -1
> > end if
> >
>
> If there can be at most one match you can use a subselect and coalesce.
>

Bruno,

Thanks for the suggestion. I've come up with the following that appears to
work:

SELECT
COALESCE((SELECT id FROM map WHERE name = $1), -1)
FROM map_level
LIMIT 1

-- Lars

--
Lars Kellogg-Stedman <lars(at)oddbit(dot)com>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-09-17 16:08:19 Re: Default value if query returns 0 rows?
Previous Message Christian Mangold 2004-09-17 15:54:17 Re: Default value if query returns 0 rows?