dbTalk Databases Forums  

[BUGS] plpgsql - variable's names conflict with table field names

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] plpgsql - variable's names conflict with table field names in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Pavel Stehule
 
Posts: n/a

Default [BUGS] plpgsql - variable's names conflict with table field names - 02-17-2004 , 04:52 AM






Hello

When I declare variable with same name as field of table, then I
have a problem with insert cmd in plpgsql procedure. I can't use this name
of columns list in insert cmd; I get syntax error.

When I use equal names in SELECT cmd, I didn't get error msg, but stored
prodedure don't work.

CREATE TABLE fog2(
idx SERIAL PRIMARY KEY,
cas TIMESTAMP
);

-- work fine
CREATE OR REPLACE FUNCTION errdemo() RETURNS TIMESTAMP AS '
DECLARE _cas TIMESTAMP;
BEGIN SELECT INTO _cas cas FROM fog2 LIMIT 1;
RETURN _cas;
END; ' LANGUAGE plpgsql;

-- don't work
CREATE OR REPLACE FUNCTION errdemo() RETURNS TIMESTAMP AS '
DECLARE cas TIMESTAMP;
BEGIN SELECT INTO cas cas FROM fog2 LIMIT 1;
RETURN cas;
END; ' LANGUAGE plpgsql;

-- works fine
CREATE OR REPLACE FUNCTION errdemo() RETURNS TIMESTAMP AS '
DECLARE cas TIMESTAMP;
BEGIN cas := CURRENT_TIMESTAMP;
INSERT INTO fog2 VALUES(DEFAULT, cas);
RETURN cas;
END; ' LANGUAGE plpgsql;

-- don't work - syntax error

CREATE OR REPLACE FUNCTION errdemo() RETURNS TIMESTAMP AS '
DECLARE cas TIMESTAMP;
BEGIN cas := CURRENT_TIMESTAMP;
INSERT INTO fog2 (cas) VALUES(cas);
RETURN cas;
END; ' LANGUAGE plpgsql;

intra=# select errdemo();
ERROR: syntax error at or near "$1" at character 20
CONTEXT: PL/pgSQL function "errdemo" line 3 at SQL statement
intra=#

Is it plpgsql error or my bug?

Regards
Pavel Stehule




---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] plpgsql - variable's names conflict with table field names - 02-17-2004 , 09:03 AM






Pavel Stehule <stehule (AT) kix (DOT) fsv.cvut.cz> writes:
Quote:
When I declare variable with same name as field of table, then I
have a problem with insert cmd in plpgsql procedure.
Don't do that.

Quote:
Is it plpgsql error or my bug?
You could argue it either way, perhaps, but it's unlikely to get
changed. In general plpgsql cannot tell whether a variable name
appearing in a SQL command ought to be substituted for or not, and so it
just always does it. I think trying to be smart would create as many
pitfalls as it'd solve. The best practice is not to use plpgsql
variable names that match table or field names you need to access in the
same procedure.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.