PLPGSQL: when the local variable used and when the table field?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

PLPGSQL: when the local variable used and when the table field?

durumdara
Hello!

PLPGSQL allows me to write simple queries and updates without execute + using.

F.e:
DECLARE t text; anytype text;
BEGIN
...
  select nev into t from anytable where type = anytype;
...
  insert into bla (id, name, type)
  select id, name, anytype from bla
...

But this method is seems to be unsafe  for me.
How the compiler knows what value I want to use?

If bla table has "anytype" field, it can use that field, or it can use local variable too.
Which has more priority/precedency?

For example:
1.) I have this proc (installed):
select nev into t from anytable where type = anytype;
Later I add anytype column to anytable.

2.) Or this:
  insert into bla (id, name, type)
  select id, name, anytype from bla
Later I add anytype to bla.

What would happen? 
The working procedure makes error, because it have more possible source of data (local variable, and table field)?
Or it uses the local variable, because in the scope it has more precedency?

Ok, sometimes I can use prefix for table:
  select bla.id, bla.name, anytype from bla  

But when I WANT to use local variable (as constant) and later the bla extended with anytype column, it would be source of conflict.

Somewhere I force to use "_" prefix for local variables to be sure in result.

Do you know any info about the background?

Thanks for answer!

Best  regards

   dd
 
Reply | Threaded
Open this post in threaded view
|

Re: PLPGSQL: when the local variable used and when the table field?

David G Johnston
On Wed, Mar 25, 2020 at 6:09 AM Durumdara <[hidden email]> wrote:

What would happen? 
The working procedure makes error, because it have more possible source of data (local variable, and table field)?
Or it uses the local variable, because in the scope it has more precedency?
[...]

Do you know any info about the background?


David J.