Re: default value returned from sql stmt - Mailing list pgsql-general

From David Salisbury
Subject Re: default value returned from sql stmt
Date
Msg-id 4F74E8A4.1030306@globe.gov
Whole thread Raw
In response to Re: default value returned from sql stmt  (Chris Angelico <rosuav@gmail.com>)
Responses Re: default value returned from sql stmt
List pgsql-general

On 3/29/12 4:26 PM, Chris Angelico wrote:
> On Fri, Mar 30, 2012 at 9:16 AM, David Salisbury<salisbury@globe.gov>  wrote:
>> development=# select  coalesce(anum,100) from t1 where anum = 4;
>
> What you have there is rather different from COALESCE, as you're
> looking for a case where the row completely doesn't exist. But you can
> fudge it with an outer join.
>
> Untested code:
>
> WITH rowid AS (select 4 as anum) SELECT coalesce(anum,100) FROM rowid
> LEFT JOIN t1 ON rowid.anum=t1.anum
>
> However, you may simply want a WHERE [NOT] EXISTS predicate. There may
> be other ways of achieving your goal, too.

Thanks guys!  In fact I did see the difference between no row and a null
value within a row.  But it seemed there must be a way that I was missing.

It does look though that plpg is the way to go, otherwise it just seems
to obfuscate the code, or have other possible consequences.

-ds

pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: default value returned from sql stmt
Next
From: Toby Corkindale
Date:
Subject: Re: How to tell if server is in backup mode?