Alex,
> create or replace function pref_money_stats(_id varchar)
> returns varchar as $BODY$
> begin
>
> declare stats varchar;
>
> for row in select yw, money from pref_money where id=_id order
> by yw desc limit 20 loop
> stats := stats || ";" || row.id || ":" || row.money;
> end loop;
>
> return stats;
> end;
> $BODY$ language plpgsql;
>
> but get the error:
>
> ERROR: syntax error at or near "for"
> LINE 7: for row in select yw, money from pref_money where id...
Your declare statement should be before the begin statement.
create or replace function pref_money_stats(_id varchar) returns varchar as
$$
declare
stats varchar;
begin
for row ...
return stats;
end;
$$ language plpgsql;