pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Boettcher <bboett@erm1.u-strasbg.fr>
To: pgsql-sql@postgresql.org
Subject: PL/PGSQL beginning is hard....
Date: Fri, 3 Nov 2000 00:58:23 +0100
Message-ID: <20001103005823.G14044@erm1.u-strasbg.fr> (raw)

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@earthling.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....



view thread (2+ messages)  latest in thread

Message-ID: <20001103005823.G14044@erm1.u-strasbg.fr>
Permalink:  ../20001103005823.G14044@erm1.u-strasbg.fr/
Also on:    postgresql.org/message-id/20001103005823.G14044@erm1.u-strasbg.fr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: bboett@erm1.u-strasbg.fr
  Subject: Re: PL/PGSQL beginning is hard....
  In-Reply-To: <20001103005823.G14044@erm1.u-strasbg.fr>
  References: <20001103005823.G14044@erm1.u-strasbg.fr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox