dbTalk Databases Forums  

Cursor for loop and bind variables

comp.databases.postgresql comp.databases.postgresql


Discuss Cursor for loop and bind variables in the comp.databases.postgresql forum.



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

Default Cursor for loop and bind variables - 05-01-2010 , 06:59 AM






Hi,

Take a look at this piece of code:

create table tmp (a integer);

create or replace function fun() returns void as $$
declare
c cursor for select 10 as x union select 20 as x;
i integer := 20;
begin
perform mod(a, 10), min(a) from tmp group by mod(a, 10); -- query
with value
perform mod(a, i), min(a), a from tmp group by mod(a, i); --
query with variable
for rec in c loop
perform mod(a, rec.x), min(a) from tmp group by mod(a,
rec.x); -- query with cursor field
end loop;
end;
$$ language 'plpgsql';

select fun();

This results in:
ERROR: column "tmp.a" must appear in the GROUP BY clause or be used
in an aggregate function
LINE 1: SELECT mod(a, $1 ), min(a) from tmp group by mod(a, $2 )
^
QUERY: SELECT mod(a, $1 ), min(a) from tmp group by mod(a, $2 )
CONTEXT: PL/pgSQL function "fun" line 8 at PERFORM

With value or variable, this statement is parsed as: "SELECT mod(a,
$1 ), min(a) from tmp group by mod(a, $1 )", with cursor field, each
occurence of "rec.x" is replaced by its own var: $1, $2.

Possible workaround is to rewrite the loop to use a variable in the
query:
for rec in c loop
i := rec.x;
perform mod(a, i), min(a) from tmp group by mod(a, i);
end loop;

But anyway... is this behaviour intentional? A bug? Or am I doing
something wrong?

Thanks.

Reply With Quote
  #2  
Old   
Jasen Betts
 
Posts: n/a

Default Re: Cursor for loop and bind variables - 05-04-2010 , 03:35 AM






On 2010-05-01, kmehkeri <kmehkeri (AT) gmail (DOT) com> wrote:
Quote:
Hi,

Take a look at this piece of code:

create table tmp (a integer);

create or replace function fun() returns void as $$
declare
c cursor for select 10 as x union select 20 as x;
i integer := 20;
you missed:
rec record;

Quote:
begin
perform mod(a, 10), min(a) from tmp group by mod(a, 10); -- query
with value
perform mod(a, i), min(a), a from tmp group by mod(a, i); --
query with variable
for rec in c loop
perform mod(a, rec.x), min(a) from tmp group by mod(a,
rec.x); -- query with cursor field
end loop;
end;
$$ language 'plpgsql';

select fun();

This results in:
ERROR: column "tmp.a" must appear in the GROUP BY clause or be used
in an aggregate function
LINE 1: SELECT mod(a, $1 ), min(a) from tmp group by mod(a, $2 )
^
QUERY: SELECT mod(a, $1 ), min(a) from tmp group by mod(a, $2 )
CONTEXT: PL/pgSQL function "fun" line 8 at PERFORM

With value or variable, this statement is parsed as: "SELECT mod(a,
$1 ), min(a) from tmp group by mod(a, $1 )", with cursor field, each
occurence of "rec.x" is replaced by its own var: $1, $2.

Possible workaround is to rewrite the loop to use a variable in the
query:
for rec in c loop
i := rec.x;
perform mod(a, i), min(a) from tmp group by mod(a, i);
end loop;

But anyway... is this behaviour intentional? A bug? Or am I doing
something wrong?

Thanks.
Feels like a bug to me.
Send it here for an expert opinion: pgsql-bugs AT postgresql.org
(it's a mailing list)







--- news://freenews.netfront.net/ - complaints: news (AT) netfront (DOT) net ---

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.