Re: Let-bindings in SQL statements

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Jon Smark <jon(dot)smark(at)yahoo(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Let-bindings in SQL statements
Date: 2012-01-26 14:41:25
Message-ID: CAFjNrYuZczVbVJ6sBSw3psAg9qYk1qHoqhFhD_hFLiXJKcw43g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 26 January 2012 15:37, Jon Smark <jon(dot)smark(at)yahoo(dot)com> wrote:

> 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
>
>
In fact now() is a little bit tricky here. now() returns the time when the
transaction started, so if you run `begin;` and call now() multiple times
(even in different queries, but within the same transaction), the function
will return the same value.

regards
Szymon

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Volodymyr Kostyrko 2012-01-26 14:49:59 Re: How to push predicate down
Previous Message Jon Smark 2012-01-26 14:37:49 Let-bindings in SQL statements