Re: SETOF come ritorno delle funzioni

Поиск
Список
Период
Сортировка
От Pavel Stehule
Тема Re: SETOF come ritorno delle funzioni
Дата
Msg-id CAFj8pRCRaPSSOyOAXfUardAk==BY2scNOdQ6KbqFFv=QSs0+ZQ@mail.gmail.com
обсуждение исходный текст
Ответ на SETOF come ritorno delle funzioni  (Piviul <piviul@riminilug.it>)
Ответы Re: SETOF come ritorno delle funzioni
Список pgsql-general
Hello

sorry, a used language in this mailing list is English language

2012/11/30 Piviul <piviul@riminilug.it>:
> Ciao a tutti, avrei bisogno di creare una funzione che restituisca un
> insieme di record. Ho visto che è possibile fare restituire ad una
> funzione una tabella di cui si definiscono i campi all'interno della
> funzione stessa
>
> CREATE FUNCTION foo() RETURNS TABLE(id INT, foo TEXT)
>
> Poi nel corpo provo a costruirmi il record da restituire con RETURN NEXT
> ma mi da un errore: RETURN NEXT non può avere un parametro in una
> funzione con parametri OUT a o vicino "r" dove "r" è la variabile di
> tipo record che vorrei accodare all'output.
>
> Vorrei in altre parole fare una funzione tipo:
>
> CREATE OR REPLACE FUNCTION magazzino.foo()
> RETURNS TABLE(id INT, nome TEXT) AS
> $pippo$
> DECLARE
>    r RECORD;
> BEGIN
>     FOR r in
>        SELECT id::int, nome::text
>        FROM foo
>     LOOP
>         RETURN NEXT r;
>     END LOOP;
>     RETURN;
> END;
> $pippo$ LANGUAGE plpgsql;
>

there should be identifier collision - you cannot simply mix plpgsql
variables and sql identifiers - so you have to use qualified
identifiers - schema.name

CREATE OR REPLACE FUNCTION magazzino.foo()
RETURNS TABLE(id INT, nome TEXT) AS
$pippo$
BEGIN
    FOR  id, nome in
       SELECT foo.id::int, foo.nome::text
       FROM foo
    LOOP
        RETURN NEXT;
    END LOOP;
    RETURN;
END;
$pippo$ LANGUAGE plpgsql;

Regards

Pavel Stehule

> Cosa sbaglio?
>
> Piviul
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general


В списке pgsql-general по дате отправления:

Предыдущее
От: "Karsten Hilbert"
Дата:
Сообщение: Re: Database schema
Следующее
От: Guillaume Lelarge
Дата:
Сообщение: Re: Bug in postgres 9.2 installation: pg_archivecleanup: command not found