Re: Let-bindings in SQL statements

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Jon Smark'" <jon(dot)smark(at)yahoo(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Let-bindings in SQL statements
Date: 2012-01-26 14:50:14
Message-ID: 006101ccdc39$d074e4b0$715eae10$@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Jon Smark
Sent: Thursday, January 26, 2012 9:38 AM
To: pgsql-general(at)postgresql(dot)org
Subject: [GENERAL] Let-bindings in SQL statements

Hi,

Is it possible to do the equivalent of let-bindings in a pure SQL function?
I have a SELECT that invokes "now" multiple times.  It would be nicer to do
it only once and reuse the value.  Something like this:

LET right_now = SELECT now () IN
SELECT * FROM my_table WHERE right_now >= start AND ...

In PL/pgSQL this is easy, but I wonder about SQL...

Thanks in advance!
Jon

-----------------------------------------------------------------------

No, not really. In some cases you can use a CTE (WITH) clause to create a
single row with whatever names and values you need and then, using
Sub-Selects or CROSS JOIN, introduce that row into the appropriate parts of
the query.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message salah jubeh 2012-01-26 16:13:05 Re: How to push predicate down
Previous Message Volodymyr Kostyrko 2012-01-26 14:49:59 Re: How to push predicate down