Re: [postgresql-it] return da funzione
- From: Chris Mair <chris@xxxxxxxx>
- To: postgresql-it <postgresql-it@xxxxxxxxxxxxx>
- Date: Thu, 09 Oct 2014 09:46:03 +0200
salve
sto cercando di farmi restituire una lista di record da una funzione
ci sono riuscito così
CREATE OR REPLACE FUNCTION public.cerca_articoli (
sito integer,
listino integer
)
RETURNS TABLE (
par1 numeric,
par2 varchar
) AS
$body$
BEGIN
RETURN QUERY
SELECT par1, par2
FROM tabella;
END;
$body$
LANGUAGE 'plpgsql';
pero' sia con il "RETURNS TABLE" oppure con
"RETURNS SETOF record AS"
SELECT * FROM cerca_articoli(41,3) AS (f1 numeric, f2 varchar)
devo mappare sempre i campi restituiti dalla funzione.
c'è modo per evitarlo?
grazie
Ciao,
io di solito mi aiuto con un type:
chris=# create type articolo_t as (f1 numeric, f2 numeric);
CREATE TYPE
chris=# create function cerca_articoli(sito int, listino int) returns setof
articolo_t as
chris-# $$
chris$# declare
chris$# rec articolo_t;
chris$# begin
chris$# for rec in select x/2, x*2 from generate_series(1,5) as x loop
chris$# return next rec;
chris$# end loop;
chris$# end;
chris$# $$ language 'plpgsql';
CREATE FUNCTION
chris=# select * from cerca_articoli(1,1);
f1 | f2
----+----
0 | 2
1 | 4
1 | 6
2 | 8
2 | 10
(5 rows)
Bye,
Chris.
--
Chris Mair
http://www.pgtraining.com
Other related posts: