Re: PL/PGSQL question - Mailing list pgsql-general

From Tom Lane
Subject Re: PL/PGSQL question
Date
Msg-id 5096.1042784448@sss.pgh.pa.us
Whole thread Raw
In response to PL/PGSQL question  (Jean-Christian Imbeault <jc@mega-bucks.co.jp>)
List pgsql-general
Jean-Christian Imbeault <jc@mega-bucks.co.jp> writes:
> I wrote the following plpgsql function. The problem I have is that if no
> rows are found my function returns NULL whereas it should be returning 0.

SUM() over no rows returns NULL, not zero, per the SQL spec.
(Yes, it's a stupid spec.)

Your IF NOT FOUND test can never succeed, because the select will always
return exactly one row no matter what.  Try testing total_sales for NULL
instead.

            regards, tom lane

pgsql-general by date:

Previous
From: Jean-Christian Imbeault
Date:
Subject: PL/PGSQL question
Next
From: Jean-Christian Imbeault
Date:
Subject: Re: PL/PGSQL question