Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C4C1C47658C for ; Wed, 18 Sep 2002 06:29:53 -0400 (EDT) Received: from anchor-post-32.mail.demon.net (anchor-post-32.mail.demon.net [194.217.242.90]) by postgresql.org (Postfix) with ESMTP id 10AB34765A0 for ; Wed, 18 Sep 2002 06:29:53 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-32.mail.demon.net with esmtp (Exim 3.35 #1) id 17rc5i-0008pO-0W; Wed, 18 Sep 2002 11:29:54 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 3851016B59; Wed, 18 Sep 2002 11:22:28 +0100 (BST) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 74B8F16B58; Wed, 18 Sep 2002 11:22:27 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: "david williams" Subject: Re: Returning a reference to a cursor from a function Date: Wed, 18 Sep 2002 11:21:25 +0100 User-Agent: KMail/1.4.1 References: In-Reply-To: Cc: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200209181121.25213.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/202 X-Sequence-Number: 9479 On Tuesday 17 Sep 2002 7:12 pm, you wrote: > Richard, > > Thanks for the information. I've made some modifications to your code here > so that it does a RAISE NOTICE in each loop returning simply the value of= n > and then when the loop is finished it again returns n. > > This works fine at the psql level but after it passes through ODBC to the > ASP layer all I get is the final RETURN value. Yep - the NOTICE is really a type of error message (you can use RAISE to=20 generate errors too) and isn't part of your data-stream. > I have tried using the RETURN function in the loop but it terminates the > loop. Indeed it does. > I really need to return each record up to the ASP layer. The solution to this sort of thing in version 7.3 is something called table= =20 functions, but I think they're limited to C at the moment, not plpgsql. With 7.2 you need to return the cursor from the function and then FETCH fro= m=20 it. An example was missed out from the 7.2.1 docs but you can see one in th= e=20 developer's docs (bottom of page): http://developer.postgresql.org/docs/postgres/plpgsql-cursors.html To hack our example a bit more the below takes a cursor-name and table name= =20 and defines a cursor for you. Note that when using it, you need to be within BEGIN...COMMIT (an explicit= =20 transaction) since the cursor returned from the function only lasts until t= he=20 end of a transaction. HTH - Richard Huxton DROP FUNCTION foo_count(refcursor, text); CREATE FUNCTION foo_count(refcursor, text) RETURNS refcursor AS ' DECLARE curs ALIAS FOR $1; tbl_name ALIAS FOR $2; BEGIN RAISE NOTICE ''cursor on table: %'',tbl_name; OPEN curs FOR EXECUTE ''SELECT * FROM '' || tbl_name; RETURN curs; END; 'language 'plpgsql'; richardh=3D> BEGIN; BEGIN richardh=3D> SELECT foo_count('fake_cursor','companies'); NOTICE: cursor on table: companies foo_count ------------- fake_cursor (1 row) richardh=3D> FETCH 3 FROM fake_cursor; co_id | co_name | co_postcode | co_lastchg -------+--------------------------+-------------+--------------------------= ----- 56 | Acme Associates GmBH | unknown | 2002-06-12=20 14:04:43.123408+01 57 | Imperial Investments Inc | unknown | 2002-06-12=20 14:04:43.123408+01 58 | Universal Associates USA | unknown | 2002-06-12=20 14:04:43.123408+01 (3 rows) richardh=3D> COMMIT;