X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 220809FB287 for ; Thu, 24 Aug 2006 15:14:17 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 40009-06 for ; Thu, 24 Aug 2006 18:13:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 6384D9FB24D for ; Thu, 24 Aug 2006 15:13:57 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.8/8.13.8) with ESMTP id k7OIDnML035400 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 24 Aug 2006 12:13:52 -0600 (MDT) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.8/8.13.8) with ESMTP id k7OIDnFd031269; Thu, 24 Aug 2006 12:13:49 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.8/8.13.8/Submit) id k7OIDn9L031268; Thu, 24 Aug 2006 12:13:49 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 24 Aug 2006 12:13:49 -0600 From: Michael Fuhr To: Ezequias Rodrigues da Rocha Cc: pgsql-sql@postgresql.org Subject: Re: Function to retrieve the Id column Message-ID: <20060824181348.GA31220@winnie.fuhr.org> References: <55c095e90608241007t3d004a14w9720cd66a60772e@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <55c095e90608241007t3d004a14w9720cd66a60772e@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.817 tagged_above=0 required=5 tests=SPF_HELO_SOFTFAIL, SPF_SOFTFAIL, UNPARSEABLE_RELAY X-Spam-Level: *** X-Archive-Number: 200608/184 X-Sequence-Number: 26142 On Thu, Aug 24, 2006 at 02:07:14PM -0300, Ezequias Rodrigues da Rocha wrote: > I have Id field that is used in all my database but my more often used > parameter is a column called "number" that I have in more than 5000 magnetic > cards, my question is. > > How could I make a function to retrieve this Id just passing the number as > parameter, just like: > > getCardId(number: varchar) Are you looking for something like this? CREATE FUNCTION getCardId(varchar) RETURNS integer AS $$ SELECT id FROM table_name WHERE number = $1; $$ LANGUAGE sql STABLE STRICT; Here's an example of how you'd call this function: SELECT getCardId('123456'); See "Server Programming" in the documentation for more information about how to write functions. http://www.postgresql.org/docs/8.1/interactive/server-programming.html http://www.postgresql.org/docs/8.1/interactive/xfunc-sql.html http://www.postgresql.org/docs/8.1/interactive/plpgsql.html -- Michael Fuhr