Re: (More) Questions about stored procedures

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: lmanorders <lmanorders(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: (More) Questions about stored procedures
Date: 2012-08-23 21:13:28
Message-ID: CAHyXU0y-mYa96aTDzJ4ML3=Vh4QcuQhvCPgzxgD8nN9bMF3yyA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Thu, Aug 23, 2012 at 4:08 PM, lmanorders <lmanorders(at)gmail(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message roman 2012-08-24 01:46:39 Re: Impatient warm standby - Recovery spam in pg_log/startup.log
Previous Message lmanorders 2012-08-23 21:08:18 (More) Questions about stored procedures