From: Jim C. Nasby <jnasby@pervasive.com>
To: Wiebe Cazemier <halfgaar@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: unique names in variables and columns in plsql functions
Date: Mon, 27 Mar 2006 08:48:04 -0600
Message-ID: <20060327144804.GV80726@pervasive.com> (raw)
In-Reply-To: <4427F7D3.3000103@gmail.com>
References: <4427F7D3.3000103@gmail.com>
On Mon, Mar 27, 2006 at 04:33:55PM +0200, Wiebe Cazemier wrote:
> Hi,
>
> In a plpgsl function, consider the following excerpt:
>
> DECLARE
> provider_id INTEGER;
> BEGIN
> provider_id := (SELECT provider_id FROM investment_products WHERE id =
> my_new.investment_product_id);
> END;
>
> After a lot of trouble, I found out this line doesn't work correctly
> with the variable name as it is. It doesn't give an error or anything,
> it just retrieves some wrong value (probably NULL). When I change the
> variable name to anything other than "provider_id", it works OK.
>
> I was somewhat surprised to discover this. Can't Postgres determine that
> the provider_id in the SELECT statement is not the same one as the variable?
Sadly, overloading variable names between plpgsql and SQL is *highly*
problematic. Because of this I *always* prefix plpgsql variables with
something, such as p_ for parameters and v_ for general variables.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jnasby@pervasive.com, halfgaar@gmail.com
Subject: Re: unique names in variables and columns in plsql functions
In-Reply-To: <20060327144804.GV80726@pervasive.com>
* 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