pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Albert REINER <areiner@tph.tuwien.ac.at>
Cc: PostgreSQL-SQL <pgsql-sql@postgresql.org>
Subject: Re: PL/pgSQL: possible parsing or documentation bug?
Date: Sat, 03 Feb 2001 17:15:51 -0500
Message-ID: <4317.981238551@sss.pgh.pa.us> (raw)
In-Reply-To: <20010131223906.A1337@frithjof>
References: <20010131223906.A1337@frithjof>

"Albert REINER" <areiner@tph.tuwien.ac.at> writes:
> create Function IdOfPossiblyNewAuthor(text) returns int4 as '
>   declare
>     id int4;
>   begin
>     select id into id from author where name = $1;
>     raise notice ''ID found: %'', id;
>     if id is null then
>       insert into author (name) values ($1);
>       select currval(''author_id_seq'') into id;
>       raise debug ''Author inserted. ID: %'', id;
>     end if;
>     return id;
>   end;
> ' language 'plpgsql' with (IsCachable);

> Logically it is clear which "id" should be parsed as the variable,
> which as author.id,

No, it is not so clear.  Consider the following:

	declare
	  x int4;
	  y int4;
	begin
	  x := ...;
	  select x + f1 into y from tab1 where ...;

The intent here is clearly to find a value tab1.f1 in tab1 and then
add the local variable x to form a value for the local variable y.

In general plpgsql will try to match an unqualified name to a variable
before it will consider whether it might be a field name.  If you don't
want that, qualify the field name:

     select author.id into id from author where name = $1;

Feel free to submit documentation updates to make this clearer...

			regards, tom lane



view thread (2+ messages)

Message-ID: <4317.981238551@sss.pgh.pa.us>
Permalink:  ../4317.981238551@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/4317.981238551@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, areiner@tph.tuwien.ac.at
  Subject: Re: PL/pgSQL: possible parsing or documentation bug?
  In-Reply-To: <4317.981238551@sss.pgh.pa.us>
  References: <20010131223906.A1337@frithjof> <4317.981238551@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox