Re: variable name in plpgsql

From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Cc: zhong ming wu <mr(dot)z(dot)m(dot)wu(at)gmail(dot)com>
Subject: Re: variable name in plpgsql
Date: 2010-09-09 00:48:47
Message-ID: 4C882EEF.2020306@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

zhong ming wu wrote:
> It seems that one cannot use the variable name that is the same as the
> column name of a relation like in the following function
>
> ------
> create or replace function bla() returns void language plpgsql as $$
> declare
> email varchar;
> begin
> select email into email from pass where id=1;
> raise notice 'pass is %',email;
> end;
> $$
> ------
> I get null value email and postgres 8.4.4 does not warn nor raise
> error on creating;
> I know for a fact that email must not be null; it's not null when I
> rename the variable
> email to something else.

All function lexical variables and parameters can be qualified with the function
name, so you can say "bla.email" to refer to the variable unambiguously. And
you can qualify a table column with the table name, like "pass.email" to make
that unambiguous.

> As far as I can see this isn't documented anywhere. Is this because
> it's common/insider knowledge? Can someone confirm?

It is documented, in
http://www.postgresql.org/docs/8.4/interactive/plpgsql-structure.html but I do
agree that it can be a challenge to find what section(s) of the manual has the
information one seeks.

-- Darren Duncan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message jackassplus 2010-09-09 00:55:56 Re: how do i count() similar items
Previous Message Craig Ringer 2010-09-09 00:47:10 Re: Copy From csv file with double quotes as null