Re: query variables

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: j0rd1 adame <jordi(at)jordi(dot)net>
Cc: Postgres mailing list general <pgsql-general(at)postgresql(dot)org>
Subject: Re: query variables
Date: 2005-01-28 05:34:27
Message-ID: 20050128053427.GA90884@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 27, 2005 at 07:29:24PM -0600, j0rd1 adame wrote:

> Is there any way to save the result of a query in a variable, so that i could
> use that value in another query??
>
> something like set VARIABLENAME = select count(1) from table;
> and then
> select 56778/VARIABLENAME as final_result;

You could do it in a PL/pgSQL function or a function written in
another procedural language. Another way would be to use a temporary
table:

CREATE TEMPORARY TABLE tmp AS SELECT count(*) AS foocount FROM foo;
SELECT 56778 / (SELECT foocount FROM tmp) AS final_result;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Neil Conway 2005-01-28 06:34:26 Re: changing sort_mem on the fly?
Previous Message Danny Lu 2005-01-28 05:05:53 initdb failed