Re: Default value if query returns 0 rows? - Mailing list pgsql-general

From Lars Kellogg-Stedman
Subject Re: Default value if query returns 0 rows?
Date
Msg-id Pine.LNX.4.44.0409171130480.8060-100000@wolery.deas.harvard.edu
Whole thread Raw
In response to Re: Default value if query returns 0 rows?  (Bruno Wolff III <bruno@wolff.to>)
Responses Re: Default value if query returns 0 rows?  (Lars Kellogg-Stedman <lars@deas.harvard.edu>)
List 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@oddbit.com>



pgsql-general by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Default value if query returns 0 rows?
Next
From: Steve Atkins
Date:
Subject: Re: Converting varchar() to text