X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 668E3329C63 for ; Mon, 20 Sep 2004 21:51:20 +0100 (BST) 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 11729-02 for ; Mon, 20 Sep 2004 20:51:11 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 79EF7329E80 for ; Mon, 20 Sep 2004 21:51:10 +0100 (BST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id DBB0E3578B; Mon, 20 Sep 2004 13:51:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id D9DE435789; Mon, 20 Sep 2004 13:51:09 -0700 (PDT) Date: Mon, 20 Sep 2004 13:51:09 -0700 (PDT) From: Stephan Szabo To: CHRIS HOOVER Cc: pgsql-sql@postgresql.org Subject: Re: Help with function In-Reply-To: Message-ID: <20040920134549.L79243@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/170 X-Sequence-Number: 19016 On Mon, 20 Sep 2004, CHRIS HOOVER wrote: > I need some help writing a simple function. > > Due to some program limitations for a program I run the db's for, I'm having > to write some simple functions to run some selects. However, I am not sure > how to have them correctly return the record(s) selected and/or how to > properly call them from sql. > > Would someone be so kind as to help me with this. > > Here is an example function: > > CREATE OR REPLACE FUNCTION "public"."test_func" (varchar) RETURNS SETOF > "public"."test_tbl" AS' > Declare > PCN varchar; > test_tbl_rec clmhdr%ROWTYPE; > > Begin > > PCN := $1; > > select into test_tbl_rec * from test_tbl where test_tbl.pcn = PCN; > return test_tbl_rec; > > end; > 'LANGUAGE 'plpgsql' VOLATILE RETURNS NULL ON NULL INPUT SECURITY INVOKER; If you want to return sets of rows, you're going to need to loop over the returned rows from the select using return next. These links may help: http://www.varlena.com/varlena/GeneralBits/26.html http://techdocs.postgresql.org/guides/SetReturningFunctions > I was trying to call this function from psql using: > select test_func('asdf'); As a side note, plpgsql set returning functions cannot be called in a select list, only in the from clause (the above links will have examples).