Re: Functions

From: John DeSoi <desoi(at)pgedit(dot)com>
To: Nick Jones <neckjonez(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Functions
Date: 2005-05-31 18:45:15
Message-ID: 48bdc0009b8b61cf729472d38273d6c4@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


On May 26, 2005, at 4:07 PM, Nick Jones wrote:

> I'm trying to figure out how functions work in PGSQL.  I've got a
> sample here but I keep getting an error and I'm not sure why.
>
> CREATE FUNCTION test2(VARCHAR) RETURNS SETOF TEXT AS
> '
> DECLARE
>  r record;
> BEGIN
> FOR r IN
>  select router_name from router where router_name ~ $1
> LOOP
> RETURN NEXT r;
> END LOOP;
> RETURN;
> END;
> ' LANGUAGE 'plpgsql';
>
> Then when I run this command:
> select * from test2('houston');
>
> I get an error:
> WARNING:  Error occurred while executing PL/pgSQL function test2
> WARNING:  line 7 at return next
> ERROR:  Attribute "r" not found
>
> I'm not sure why I get this error, i thought I declared it in the
> declare section.
>
>

r has the structure of table in your select, namely 'router'. Try

RETURN NEXT r.router_name;

John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL

In response to

  • Functions at 2005-05-26 20:07:36 from Nick Jones

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2005-05-31 18:47:21 Re: Functions
Previous Message operationsengineer1 2005-05-31 18:32:29 Re: Carriage Returns Disappearing