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

Started by Durumdaraabout 6 years ago2 messagesgeneral
Jump to latest
#1Durumdara
durumdara@gmail.com

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

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Durumdara (#1)
Re: PLPGSQL: when the local variable used and when the table field?

On Wed, Mar 25, 2020 at 6:09 AM Durumdara <durumdara@gmail.com> 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?

https://www.postgresql.org/docs/12/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

David J.