the select query returns the first row to rec. You can then access its values with:
rec.field_name
at END LOOP it jumps back to FOR checks to see if there any more rows and if so moves
to the next row and repeats the loop.
It also looks like your missing a LOOP keyword at the end of the FOR line
Try:
create function foo(text) returns int4 as '
delcare
tcount int4:=0;
begin
for rec IN select title, dcount from songs where artist=$1 LOOP
tcount:= tcount+rec.dcount;
END LOOP;
return tcount;
end;
' language 'plpgsql';
call it by:
db> select foo('Najm Hashmi');
it will return how
the select query returns the first row to rec. You can then access its values with:
rec.field_name
at END LOOP it jumps back to FOR checks to see if there any more rows and if so moves
to the next row and repeats the loop.
It also looks like your missing a LOOP keyword at the end of the FOR line
Jie Liang wrote:
> I just know you can use implict cursor inside the plpgsql
> e.g
> declare
result text;
tcount int4;
>
> rec record;
> begin
> FOR rec IN select_clause LOOP
> statements
> END LOOP;
> end;
>
Thank you Jie for your help. I am bit confused about how it works. I want for
ea
I just know you can use implict cursor inside the plpgsql
e.g
declare
rec record;
begin
FOR rec IN select_clause LOOP
statements
END LOOP;
end;
Jie LIANG
St Bernard Software
Internet Products Inc.
10350 Science Center Drive
Suite 100, San Diego, CA 92121
Office:(858)320-4873
[EMAIL PRO
Hi all,
Here is a plpgsql function:
flipr'#create function test_cur() returns text as'
flipr'# declare
flipr'# mycur cursor for select title from songs where song_id=10;
flipr'# usrrecord;
flipr'# resultstext;
flipr'# begin
flipr'# open mycur;
flipr'# fetch next from mycur into