Thread: Newbie help needed on Functions

Newbie help needed on Functions

From
"Jim Steil"
Date:
Hi:
 
I want to create a function that will return rows from a table based upon a parameter that I pass.
 
TABLE - NAMES
int ID,
varchar(50) NAME,
varchar(50) ADDRESS,
char(2) STATE
 
I would like my function to return the same result as it would if I entered the following SQL statement:
 
select ID, NAME, ADDRESS from NAMES where STATE = 'WI';
 
I want to pass 'WI' as a parameter.  I have looked through the programming guide but am confused by the return type and such.  Any help or pointers to further documentation would certainly be appreciated.
 
    -Jim

Re: Newbie help needed on Functions

From
Masse Jacques
Date:

I'm not an expert, but I think that a function cannot directly return a rows set. You have to use a cursor

http://www.postgresql.org/idocs/index.php?plpgsql-cursors.html

 I use the following (postgresql 7.2) to  get multiple rows in a table, but the table must exist before you run the function.

Don't forget double quotes (1+1) for the variable in the query

You have to install pl/pgsql language before ...

CREATE FUNCTION test(varchar) RETURNS varchar AS '

declare str alias for $1;

declare str_ok varchar(2);

begin

str_ok = 'OK'

drop table tmp_result;

create table tmp_result as select a.*  from your_table a where a.state=''str'';

return str_ok;

end;

' LANGUAGE 'pl/pgsql';

 

___________________________________________
Jacques Massé                                                 
Tel. 33 (0)5 57 89 08 11 - Fax 33 (0)5 57 89 08 01

-----Message d'origine-----
De : Jim Steil [mailto:jim.steil@customcall.com]
Envoyé : mardi 24 septembre 2002 19:59
À : pgsql-novice@postgresql.org
Objet : [NOVICE] Newbie help needed on Functions

Hi:
 
I want to create a function that will return rows from a table based upon a parameter that I pass.