X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 573C9D862A; Tue, 30 Aug 2005 14:59:13 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24771-02; Tue, 30 Aug 2005 17:59:11 +0000 (GMT) Received: from ns2.instrumentalyst.com (blk-222-55-203.eastlink.ca [24.222.55.203]) by svr1.postgresql.org (Postfix) with ESMTP id F2FEDD8610; Tue, 30 Aug 2005 14:59:10 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by ns2.instrumentalyst.com (Postfix) with ESMTP id 24EDD4B9; Tue, 30 Aug 2005 14:58:31 -0300 (ADT) Received: from ns2.instrumentalyst.com ([127.0.0.1]) by localhost (ns2.instrumentalyst.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 33474-09; Tue, 30 Aug 2005 14:58:27 -0300 (ADT) Received: from [192.168.2.2] (pcp842727pcs.beridg01.fl.comcast.net [68.56.170.170]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ns2.instrumentalyst.com (Postfix) with ESMTP id 4FC843BD; Tue, 30 Aug 2005 14:58:27 -0300 (ADT) Message-ID: <43149E68.8030600@productivitymedia.com> Date: Tue, 30 Aug 2005 13:59:04 -0400 From: Postgres Admin User-Agent: Thunderbird 1.6a1 (Macintosh/20050829) MIME-Version: 1.0 Cc: pgsql-sql@postgresql.org, PostgreSQL - Admin Subject: Re: [SQL] plpgsql question References: <1C7C5DE3-5EB5-4EB8-A187-290EF5081BD7@sitening.com> <4314863A.3030107@productivitymedia.com> <20050830165013.GA57465@winnie.fuhr.org> In-Reply-To: <20050830165013.GA57465@winnie.fuhr.org> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: amavisd-new at instrumentalyst.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.116 required=5 tests=[AWL=0.066, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200508/390 X-Sequence-Number: 18709 I have data in one table called articles and I would like to make a function in which takes certain data from it and display the results. Example: CREATE TABLE articles ( article_id serial, title varchar(200), posted timestamp, article_subject varchar(200), article_body text, allow_comments boolean, comments smallint ); I understand one way to display a results I would like is creating a TYPE with the columns needed. CREATE TYPE articles_output AS ( article_id int title varchar(200), article_body text, comments smallint ); Now I would like the function to display data using the LIMIT and OFFSET option ex: SELECT title, article_body, comments FROM articles ORDER BY article_id DESC *LIMIT 4 OFFSET 0*; this is function I created: CREATE OR REPLACE FUNCTION article_display(integer, integer) RETURNS SETOF article_output AS $$ DECLARE articleRow article_output%ROWTYPE; sampleRow RECORD; BEGIN FOR sampleRow IN SELECT title, article_body, comments FROM articles ORDER BY article_id DESC LIMIT $1 OFFSET $2 LOOP RETURN NEXT sampleRow; END LOOP; RETURN; END; $$ LANGUAGE plpgsql; this is the error -> ERROR: set-valued function called in context that cannot accept a set CONTEXT: PL/pgSQL function "article_sample" line 10 at return next Can I do this.... or are there better options? Thanks for the help, J