PGSQL function question

From: Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>
To: pgsql-general(at)postgresql(dot)org
Subject: PGSQL function question
Date: 2003-02-27 13:22:10
Message-ID: 3E5E1102.4040308@mega-bucks.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have created a pgpsql function but it does not return the expected
value. It's a simple sql query, and if I do the same query by hand I get
the expected result so I can't see what is wrong.

Can someone offer some advice? Thanks :)

My function:

create or replace function member_points_to_be_refunded(integer) returns
integer as '
declare points_used integer := 0;
begin
SELECT into points_used sum(points_used)
FROM invoices
WHERE
member_id=$1
AND payment_rcvd
AND cancelled;
IF points_used IS NULL THEN
RETURN 0;
END IF;
RETURN points_used;
end;
' language 'plpgsql' with (iscachable);

My tests:

TAL=# select member_points_to_be_refunded(9);
member_points_to_be_refunded
------------------------------
0
(1 row)

TAL=# select sum(points_used) from invoices where member_id=9 and
payment_rcvd and cancelled;
sum
-----
100
(1 row)

TAL=# select points_used, cancelled from invoices where id=52;
points_used | cancelled
-------------+-----------
100 | t
(1 row)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Sullivan 2003-02-27 13:36:41 Re: 7.4?
Previous Message Roberto de Amorim 2003-02-27 12:25:17 Function example returning more then 1 value