Re: Function to retrieve the Id column

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Ezequias Rodrigues da Rocha <ezequias(dot)rocha(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Function to retrieve the Id column
Date: 2006-08-24 18:13:49
Message-ID: 20060824181348.GA31220@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Scott Petersen 2006-08-24 18:35:57 Re: Deleting Functions
Previous Message Tom Lane 2006-08-24 17:29:46 Re: double precision vs. numeric