Re: Setting a default value for a select statement without results

From: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
To: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Setting a default value for a select statement without results
Date: 2013-02-06 00:48:17
Message-ID: CAB8KJ=iG_zZ+3sui5u8t48YxTb7-CU+JUDu=pzH4_C6b8M9EXA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2013/2/6 JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>:
> I have an UPDATE query with the following general structure:
>
> UPDATE table1 SET (SELECT field FROM table2 WHERE conditions ORDER BY
> order_field LIMIT 1)
>
> Is it possible to assign a default value in case no results are returned by
> the SELECT statement?

One option would be to do something with UNION along these lines:

UPDATE table1 SET field =
(SELECT field FROM table2 WHERE conditions
UNION
SELECT 'default_value' WHERE NOT EXISTS (SELECT field FROM table2
WHERE conditions )
ORDER BY order_field LIMIT 1
)

HTH

Ian Barwick

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Wayne Cuddy 2013-02-06 01:13:49 index scan vs bitmap index scan
Previous Message Anton Gavazuk 2013-02-06 00:35:03 Re: Setting a default value for a select statement without results