Re: returning more than one value from a function

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: returning more than one value from a function
Date: 2010-05-01 03:46:30
Message-ID: hrg86m$186$1@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 2010-04-30, Lonni J Friedman <netllama(at)gmail(dot)com> wrote:
> Greetings,
> I'm attempting to create a PL/PGSQL function with an IF/THEN
> conditional. Basically, all I really need is the ability to run a
> long/complex SQL query based on the value of the newest row of one
> column in a specific table (if its equal to 1 then run the SQL, if
> anything else don't run it). In pseudo code, something like:
>
> CREATE OR REPLACE FUNCTION foo0 RETURN text AS $$
> IF (SELECT current_status from table0 WHERE id in (SELECT max(id) FROM
> table0))='1' THEN
> <LONG SQL QUERY>
> END IF;
> LANGUAGE 'plpgsql' ;
>
>

> This seems like a fairly simple requirement, yet I can't find any way
> to do this without creating a function.

> The problem is that the
> long/complex SQL query needs to return multiple columns of output (not
> just a single value), and functions can only return a single
> value/column, so I'm a bit stuck on how to make this work.
>
> Is there an alternative solution that I'm missing?

functions can return multple columns.

or you could try this:

SELECT * FROM (

<long sql query>

) AS foo WHERE

((SELECT current_status from table0 WHERE id in (SELECT max(id) FROM table0))='1')

;

the planner should be is smart enough to see that the where clause is
independant to the from clause and immutable and so evaluate it once.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Didier Gasser-Morlay 2010-05-01 08:53:01 Re: Last modification date for Triggers, functions, tables ....
Previous Message Jasen Betts 2010-05-01 03:33:34 Re: Last modification date for Triggers, functions, tables ....