Cursors
Cursor query with variables
Get the total balances for a given account type.
create function total_balances(type varchar) returns numeric(10, 2) as $$
declare
account_cursor cursor(t varchar) for
select *
from account
where account_type = t;
acct account;
total numeric(10, 2) = 0;
begin
for acct in account_cursor(type) loop
total = total + acct.balance;
end loop;
return total;
end
$$ language plpgsql;
...
select total_balances('checking');
total_balances
----------------
1100.00
|
|
|