Re: Help with function

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: CHRIS HOOVER <CHRIS(dot)HOOVER(at)companiongroup(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with function
Date: 2004-09-20 20:51:09
Message-ID: 20040920134549.L79243@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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).

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Chester Kustarz 2004-09-20 20:55:38 Re: COUNT(*) to find records which have a certain number of
Previous Message Greg Stark 2004-09-20 20:45:48 Re: COUNT(*) to find records which have a certain number of dependencies ?