PL/pgSQL and SETOF

From: Cultural Sublimation <cultural_sublimation(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: PL/pgSQL and SETOF
Date: 2007-11-30 17:09:28
Message-ID: 207655.45506.qm@web63404.mail.re1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I am having trouble getting a really simple PL/pgSQL function to work.
I am beginning to wonder if there is not a bug somewhere, or if Postgresql's
type system is not broken. Anyway, suppose I have the following table and
type defined:

CREATE TABLE items
(
item_id int,
item_name text,
item_etc text
);

CREATE TYPE simple_item_t AS
(
item_id int,
item_name text
);

It's easy to create a SQL function that returns a set of simple items:

CREATE FUNCTION get_items ()
RETURNS SETOF simple_item_t
LANGUAGE sql STABLE AS
$$
SELECT item_id, item_name FROM items;
$$;

Now, all I want is to create the equivalent PL/pgSQL function. Nothing
more, nothing less. This is the simplest version I can come up with:

CREATE FUNCTION get_items2 ()
RETURNS SETOF simple_item_t
LANGUAGE plpgsql STABLE AS
$$
DECLARE
item simple_item_t%ROWTYPE;
BEGIN
FOR item IN SELECT item_id, item_name FROM items
LOOP
RETURN NEXT item;
END LOOP;
RETURN;
END
$$;

Unfortunately it doesn't work! Postgresql complains that "set-valued
function called in context that cannot accept a set". Anyway, what am
I doing wrong, and what is the simplest way of translating get_items
in PL/pgSQL?

Thanks in advance!
C.S.

____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Guido Neitzer 2007-11-30 17:11:49 Re: Linux v.s. Mac OS-X Performance
Previous Message Guido Neitzer 2007-11-30 16:59:03 Re: Postgres High Availablity Solution needed for hot-standby and load balancing