Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 904229FB33A for ; Sun, 15 Oct 2006 23:41:02 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 34384-03-2 for ; Sun, 15 Oct 2006 23:41:02 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from hub.org (hub.org [200.46.204.220]) by postgresql.org (Postfix) with ESMTP id 4DC009FB30C for ; Sun, 15 Oct 2006 23:41:02 -0300 (ADT) Received: from localhost (unknown [200.46.208.251]) by hub.org (Postfix) with ESMTP id B737A3A4656 for ; Sun, 15 Oct 2006 23:40:52 -0300 (ADT) Received: from hub.org ([200.46.204.220]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 33841-04 for ; Sun, 15 Oct 2006 23:39:46 -0300 (ADT) Received: from ganymede.hub.org (blk-137-86-60.eastlink.ca [24.137.86.60]) by hub.org (Postfix) with ESMTP id 0AB953A4653 for ; Sun, 15 Oct 2006 23:39:37 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by ganymede.hub.org (Postfix) with ESMTP id BAC93343E9 for ; Sun, 15 Oct 2006 23:39:51 -0300 (ADT) Date: Sun, 15 Oct 2006 23:39:51 -0300 From: "Marc G. Fournier" To: pgsql-sql@postgresql.org Subject: Recursive pl/pgsql function ... Message-ID: X-Mailer: Mulberry/4.0.6 (Linux/x86) MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="==========22E0D35A5E145F64F9B2==========" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200610/148 X-Sequence-Number: 26537 --==========22E0D35A5E145F64F9B2========== Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 'k, this isn't quite working as expected (or, rather, hoped) ... I've created a = recursive function (that does work nicely) that takes a date in the past, and=20 keeps advancing in steps of 'n months' until the date is in the future: CREATE OR REPLACE FUNCTION get_next_billing_date(timestamp with time zone,=20 int4) RETURNS TIMESTAMP AS ' DECLARE max_date RECORD; ret TIMESTAMP WITH TIME ZONE; start_date ALIAS FOR $1; payment_period ALIAS FOR $2; BEGIN SELECT INTO max_date CASE WHEN now() < start_date + ( payment_period || ''=20 months'')::interval THEN payment_period ELSE NULL END; IF max_date.case IS NULL THEN SELECT INTO ret get_next_billing_date(start_date + ( payment_period || ''=20 months'')::interval, payment_period); ELSE RETURN start_date + ( payment_period || '' months'')::interval; END IF; RETURN ret; END; ' LANGUAGE plpgsql; It works, no problem there: # select get_next_billing_date('2005-10-15', 3); get_next_billing_date ----------------------- 2007-01-15 00:00:00 (1 row) But, as soon as I try and use that function as a field in a query, it gives an=20 error: 1 Record: # select get_next_billing_date(activated, 12) from company_details where=20 company_id =3D 771; get_next_billing_date ---------------------------- 2007-05-03 15:09:19.491958 (1 row) More then one Record: # select get_next_billing_date(activated, 12) from company_details; ERROR: control reached end of function without RETURN CONTEXT: PL/pgSQL function "get_next_billing_date" Something I've written wrong in the function, or just not something that is=20 doable? Thanks ... ---- Marc G. Fournier Hub.Org Networking Services (http://www.hub.org) Email . scrappy@hub.org MSN . scrappy@hub.org Yahoo . yscrappy Skype: hub.org ICQ . 7615664 --==========22E0D35A5E145F64F9B2========== Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (FreeBSD) iD8DBQFFMvD34QvfyHIvDvMRAneoAKDJvwMeSKGFO651i1E526w3lyDjcQCg3pUu TAEdVkHySEFC+Yr6uQJyYJQ= =wjhY -----END PGP SIGNATURE----- --==========22E0D35A5E145F64F9B2==========--