PL/PGSQL beginning is hard....

From: Bruno Boettcher <bboett(at)erm1(dot)u-strasbg(dot)fr>
To: pgsql-sql(at)postgresql(dot)org
Subject: PL/PGSQL beginning is hard....
Date: 2000-11-02 23:58:23
Message-ID: 20001103005823.G14044@erm1.u-strasbg.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

hello,

i am a beginner at SQL and PL/pgsql.... and thus have some surely
already known problems...

i have set up some tables, and wanted to play around with inbuild
functions, and set up the following function:

CREATE FUNCTION balance (int4) RETURNS int4 AS '
DECLARE
compte ALIAS FOR $1;
actplus accounts.num%TYPE;
actminus accounts.num%TYPE;
actres accounts.num%TYPE;
BEGIN
SELECT SUM(amount) INTO actplus FROM journal WHERE plus=compte;
select sum(amount) INTO actminus from journal where minus=compte;
actres := actplus - actminus;
RETURN actres;
END;
' LANGUAGE 'plpgsql';

Now this works fine, until it hits one of the cases where either of the
selects returns an empty result (meaning that no line conforming to the
contraint could be found) in this case even if the other select returns
a value, the whole function does return nothing....

what did i wrong, or what do i have to change, to assume the value 0 if
no hit was found to the select?

BTW i am wondering if the same thing could have been achieved with sole
SQL, and if yes, how....

--
ciao bboett
==============================================================
bboett(at)earthling(dot)net
http://inforezo.u-strasbg.fr/~bboett http://erm1.u-strasbg.fr/~bboett
===============================================================
the total amount of intelligence on earth is constant.
human population is growing....

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2000-11-03 00:49:18 Re: PL/PGSQL beginning is hard....
Previous Message Tom Lane 2000-11-02 17:17:05 Re: Re(2): Re(2): Large Object dump ?