Re: Setting a default value for a select statement without results - Mailing list pgsql-sql

From Ian Lawrence Barwick
Subject Re: Setting a default value for a select statement without results
Date
Msg-id CAB8KJ=iG_zZ+3sui5u8t48YxTb7-CU+JUDu=pzH4_C6b8M9EXA@mail.gmail.com
Whole thread Raw
In response to Setting a default value for a select statement without results  (JORGE MALDONADO <jorgemal1960@gmail.com>)
List pgsql-sql
2013/2/6 JORGE MALDONADO <jorgemal1960@gmail.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
(SELECTfield FROM table2
 
WHERE conditions )  ORDER BY order_field LIMIT 1 )

HTH

Ian Barwick



pgsql-sql by date:

Previous
From: Anton Gavazuk
Date:
Subject: Re: Setting a default value for a select statement without results
Next
From: Wayne Cuddy
Date:
Subject: index scan vs bitmap index scan