Re: [GENERAL] PL/pgSQL syntax/usage question

From: Ed Loehr <eloehr(at)austin(dot)rr(dot)com>
To: Henk van Lingen <henkvl(at)cs(dot)uu(dot)nl>
Cc: pgsql-general(at)hub(dot)org
Subject: Re: [GENERAL] PL/pgSQL syntax/usage question
Date: 2000-02-01 15:36:48
Message-ID: 3896FD90.AEAFDAF1@austin.rr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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?

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ed Loehr 2000-02-01 15:43:51 Re: [HACKERS] float4 confused as int??
Previous Message Arnaud FLORENT 2000-02-01 15:33:57 VACUUM ANALYSE