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 F0F3E3A1EC1 for ; Mon, 29 Nov 2004 22:26:07 +0000 (GMT) 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 03263-10 for ; Mon, 29 Nov 2004 22:25:56 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id C820F3A5231 for ; Mon, 29 Nov 2004 22:25:57 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iATMPqTj019666 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 29 Nov 2004 15:25:54 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iATMPpVD079870; Mon, 29 Nov 2004 15:25:51 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iATMPorg079869; Mon, 29 Nov 2004 15:25:50 -0700 (MST) (envelope-from mfuhr) Date: Mon, 29 Nov 2004 15:25:50 -0700 From: Michael Fuhr To: RobertD.Stewart@ky.gov Cc: pgsql-sql@postgresql.org Subject: Re: stored procedures in postgresql user plpgsql Message-ID: <20041129222550.GA67787@winnie.fuhr.org> References: <06AF099D94D33B4D9120504521D6539D0F47CD96@agency33.state.ky.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <06AF099D94D33B4D9120504521D6539D0F47CD96@agency33.state.ky.us> 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: 200411/288 X-Sequence-Number: 19606 On Mon, Nov 29, 2004 at 01:37:42PM -0500, RobertD.Stewart@ky.gov wrote: > I have tried to create a simple function to select data from a table. Could > you all please correct me It would be helpful if you told us what you want to happen and what actually does happen. Without that information we have to guess at your intentions. > CREATE FUNCTION retrive_atype(varchar) RETURNS varchar AS ' > BEGIN > RETURN select username from masteraccount where atype=$1; > END; > ' LANGUAGE 'plpgsql'; > > To call the function I used > > Select retrive_atype(); You've defined the function to take a VARCHAR argument but you called it without an argument. Also, you've defined it to return a single VARCHAR value, but if the query could return multiple values then the function should return SETOF VARCHAR. Maybe this is closer to what you need: CREATE FUNCTION retrive_atype(VARCHAR) RETURNS SETOF VARCHAR AS ' DECLARE rec RECORD; BEGIN FOR rec IN SELECT username FROM masteraccount WHERE atype = $1 LOOP RETURN NEXT rec.username; END LOOP; RETURN; END; ' LANGUAGE plpgsql; You'd call the function like this: SELECT * FROM retrive_atype('some-atype-value'); If that's not what you're looking for then please provide more details. BTW, is "retrive" supposed to be "retrieve"? -- Michael Fuhr http://www.fuhr.org/~mfuhr/