|< < 50 > >|

Cursors

Using a query without a for loop

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 open account_cursor(type); loop fetch account_cursor into acct; exit when not found; total = total + acct.balance; end loop; return total; end $$ language plpgsql; ... select total_balances('checking'); total_balances ---------------- 1100.00

|< < 50 > >|