dbTalk Databases Forums  

Why this does not work ??

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Why this does not work ?? in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Roberto Rezende de Assis
 
Posts: n/a

Default Why this does not work ?? - 07-03-2004 , 01:22 PM






Hello all, why this does not work ?

-----------------------------------------
create table original(num int);
-----------------------------------------
create table copia(num int);
-----------------------------------------
insert into original(num) values (1);
insert into original(num) values (2);
insert into original(num) values (3);
-----------------------------------------
create function copiar()
returns int as '
declare
ponteiro int;
begin
for ponteiro in select * from original order by num asc loop
insert into copia(num) values(ponteiro);
end loop;
return 1;
end;
' language plpgsql;
-----------------------------------------
-----------------------------------------
-----------------------------------------
This are the results
teste=# \i for.sql
CREATE TABLE
CREATE TABLE
INSERT 127676 1
INSERT 127677 1
INSERT 127678 1
CREATE FUNCTION
teste=# select copiar();
WARNING: plpgsql: ERROR during compile of copiar near line 4
ERROR: missing .. at end of SQL expression
teste=#



---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Roberto Rezende de Assis
 
Posts: n/a

Default Re: Why this does not work ?? (Now it has worked) - 07-03-2004 , 02:08 PM






Thank you all, that one here is the one that worked

*declare*
/r/ record;
*begin
for* /r/ *in* /*select* * *from* original *order by* num *asc*/ *loop*
*insert into* copia(num) *values*(r.num);
*end loop*;
*end;
*




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

Default Re: Why this does not work ?? - 07-03-2004 , 04:56 PM



Roberto Rezende de Assis <rezende_assis (AT) yahoo (DOT) com.br> writes:
Quote:
Hello all, why this does not work ?

create function copiar()
returns int as '
declare
ponteiro int;
begin
for ponteiro in select * from original order by num asc loop
insert into copia(num) values(ponteiro);
The loop variable of a for/select loop has to be a record or rowtype
variable. So you should do something like

declare
r record;
begin
for r in select * from original order by num asc loop
insert into copia(num) values(r.num);

Quote:
WARNING: plpgsql: ERROR during compile of copiar near line 4
ERROR: missing .. at end of SQL expression
I agree that this error message is not very helpful :-(

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
  #4  
Old   
Tom Lane
 
Posts: n/a

Default Re: Why this does not work ?? - 07-03-2004 , 10:07 PM



I wrote:
Quote:
The loop variable of a for/select loop has to be a record or rowtype
variable. So you should do something like

declare
r record;
begin
for r in select * from original order by num asc loop
insert into copia(num) values(r.num);

WARNING: plpgsql: ERROR during compile of copiar near line 4
ERROR: missing .. at end of SQL expression

I agree that this error message is not very helpful :-(
FYI, I have just committed some fixes that will hopefully provide more
helpful error messages for erroneous FOR-loops. Your example will
draw

ERROR: loop variable of loop over rows must be a record or row variable

in PG 7.5.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #5  
Old   
Markus Bertheau
 
Posts: n/a

Default Re: Why this does not work ?? - 07-04-2004 , 05:10 AM



В Сбт, 03.07.2004, в 23:56, Tom Lane пишет:
Quote:
Roberto Rezende de Assis <rezende_assis (AT) yahoo (DOT) com.br> writes:
Hello all, why this does not work ?

create function copiar()
returns int as '
declare
ponteiro int;
begin
for ponteiro in select * from original order by num asc loop
insert into copia(num) values(ponteiro);

The loop variable of a for/select loop has to be a record or rowtype
variable. So you should do something like

declare
r record;
begin
for r in select * from original order by num asc loop
insert into copia(num) values(r.num);

WARNING: plpgsql: ERROR during compile of copiar near line 4
ERROR: missing .. at end of SQL expression

I agree that this error message is not very helpful :-(
FWIW, the documentation explicitly states this case and the unhelpful
error message it yields.

I haven't looked, but the docs would need fixing then, too.

Thanks.

--
Markus Bertheau <twanger (AT) bluetwanger (DOT) de>


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



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.