Re: [GENERAL] PL/pgSQL syntax/usage question - Mailing list pgsql-general

From Ed Loehr
Subject Re: [GENERAL] PL/pgSQL syntax/usage question
Date
Msg-id 3896FD90.AEAFDAF1@austin.rr.com
Whole thread Raw
In response to PL/pgSQL syntax/usage question  (Henk van Lingen <henkvl@cs.uu.nl>)
List pgsql-general
A couple of issues here...

First, your actual return type is 'record', but you declared it as
'text'.  If it were possible, you'd probably want something like this:

    select into result lname from tabel where ...
    return result.lname;

Second, IIRC, you cannot pass in a string representing the tablename
and then use it as a tablename in the select (though it would be quite
handy).  Not sure how to do it, though.

Cheers,
Ed Loehr


Henk van Lingen wrote:
>
> Hi *,
>
> I'm starting with PL/pgSQL but can't exactly find out how to do
> something like this:
>
> CREATE FUNCTION textkey (text, int4) RETURNS text AS '
>         DECLARE
>                 tabel ALIAS FOR $1;
>                 id ALIAS FOR $2;
>                 result record;
>         BEGIN
>                 select lname into result from tabel
>                 where person_id=id;
>                 return result;
>         END;
>     ' LANGUAGE 'plpgsql';
>
> csbase=> select textkey ('person',111) as result;
> ERROR:  parser: parse error at or near "$1"
>
> postmaster.log:
>
> StartTransactionCommand
> query: select textkey ('person',111) as result;
> ProcessQuery
> query: SELECT lname from $1 where person_id=$2
> ERROR:  parser: parse error at or near "$1"
> DEBUG:  Last error occured while executing PL/pgSQL function textkey
> DEBUG:  line 6 at select into variables
> AbortCurrentTransaction
>
> How does one do things like this? Why aren't the $n variables expanded
> in the logfile?
>
> Maybe it is documented but I can't find it in chapter 11 of the Programmer's
> Guide. Are there other places where PL/pgSQL is explained?

pgsql-general by date:

Previous
From: Arnaud FLORENT
Date:
Subject: VACUUM ANALYSE
Next
From: Ed Loehr
Date:
Subject: Re: [GENERAL] VACUUM ANALYSE