On Thu, Aug 23, 2012 at 4:08 PM, lmanorders <lmanorders@gmail.com> wrote:
> I'm still experimenting with stored procedures and I have a couple of
> questions that I can't seem to find answers to. Here is the function
>
> CREATE OR REPLACE FUNCTION getdetailamts ( acct_no char(22), beg_moyr
> char(6),
> end_moyr char(6), OUT beg_bal float8, OUT half_bal float8) AS $$
> DECLARE recs record;
> BEGIN
> beg_bal := 0.0; -- if this is left out the later addition doesn't
> occur
> half_bal := 0.0;
> FOR recs IN SELECT apmoyr, drtype, dramt FROM detrec WHERE
> detrec.acctno = $1 AND detrec.apmoyr >= $2 AND detrec.apmoyr <= $3"
> LOOP
> beg_bal := beg_bal + recs.dramt;
> END LOOP;
> END; $$ language plpgsql IMMUTABLE STRICT;
>
> My first question is that if I don't initially set beg_bal := 0.0, the
> addition in the loop doesn't seem to work. It returns NULL. I don't see
> anything in the documentation that explains this behavior. I assume it has
> something to do with beg_bal's initial value being NULL?
that is correct (and any subsequent calculation then returns null)
> I change the loop part to:
>
> LOOP
> IF recs.apmoyr < $3 THEN beg_bal := beg_bal + recs.dramt;
> END LOOP;
>
> Now I get an error when I attempt to create the function. Why?
structure of if statements is If..then..end if;
LOOP
IF recs.apmoyr < $3 THEN
beg_bal := beg_bal + recs.dramt;
END IF;
END LOOP;
merlin