pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Listas Evandro (Não use este endereço para mensagens pessoais) <listasjr@gmail.com>
To: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Select of a function that returns a array
Date: Thu, 5 May 2005 15:38:29 +0100
Message-ID: <ffac568905050507383026a00b@mail.gmail.com> (raw)
In-Reply-To: <ffac5689050505072721405cb4@mail.gmail.com>
References: <ffac5689050505072721405cb4@mail.gmail.com>

Hi guys

I'm trying to figure out a way to make this select work even for a wrong value:

select mean_and_variancepop('golf', 'temperature');
or
select CAST (mean_and_variancepop('golf', 'temperature') AS float(2));

This function return an array

the function is:

CREATE or replace FUNCTION mean_and_variancepop(var_table varchar,
var_col varchar) RETURNS float(2) AS $$
DECLARE
   curs refcursor;
   a float;
   b float;
   c float;
   retvals float[2];
   sum float:=0;
BEGIN
   open curs FOR EXECUTE 'SELECT ' || quote_ident(var_col) || ' FROM
' || quote_ident(var_table);
   b:=0;

LOOP
  -- some computations
FETCH curs INTO a;

   IF  NOT FOUND THEN
       EXIT;  -- exit loop
   END IF;
   b:=b+1;
   c:=a;
   sum:= sum + c;
END LOOP;
   CLOSE curs;
   retvals[0]:= sum / b;
   retvals[1] :=4;
   RETURN retvals;

END;
$$ LANGUAGE plpgsql;

Thanks
-Jr



view thread (3+ messages)  latest in thread

Message-ID: <ffac568905050507383026a00b@mail.gmail.com>
Permalink:  ../ffac568905050507383026a00b@mail.gmail.com/
Also on:    postgresql.org/message-id/ffac568905050507383026a00b@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: listasjr@gmail.com
  Subject: Re: Select of a function that returns a array
  In-Reply-To: <ffac568905050507383026a00b@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox