ECPG and Curors.

Started by Peter L. Bergholdalmost 20 years ago3 messagesgeneral
Jump to latest
#1Peter L. Berghold
Peter@berghold.net

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm looking at the documentation for Postgresql in Chapter 30 and I'm
checking out how to use FETCH INTO and CURSORs to loop through multiple
results from a table.

In the documentation they show something like

EXEC SQL DECLARE foo CURSOR for select a,b,c from test_table

do {
EXEC SQL FETCH NEXT FROM foo INTO ....

} while ( ... );

what I don't see is how to detect that I've fetched the last row from a
query. Is there more complete doco on this process somewhere?

- --
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Peter L. Berghold Peter@Berghold.Net
"Those who fail to learn from history are condemned to repeat it."
AIM: redcowdawg Yahoo IM: blue_cowdawg ICQ: 11455958
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Red Hat - http://enigmail.mozdev.org

iD8DBQFEji2QUM9/01RIhaARAqV6AJ43/F6y5sKbvY837dVwNL8ZPz0MxgCeIJlL
5Fo3FyR3e5Aup53s/z0UrxY=
=RLxy
-----END PGP SIGNATURE-----

#2Michael Meskes
meskes@postgresql.org
In reply to: Peter L. Berghold (#1)
Re: ECPG and Curors.

On Mon, Jun 12, 2006 at 11:14:24PM -0400, Peter L. Berghold wrote:

what I don't see is how to detect that I've fetched the last row from a
query. Is there more complete doco on this process somewhere?

Just look for "exec sql whenever not found ..."

Michael

--
Michael Meskes
Email: Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!

#3John Smith
john@roundel.net
In reply to: Michael Meskes (#2)
Re: ECPG and Curors.

On Mon, Jun 12, 2006 at 11:14:24PM -0400, Peter L. Berghold wrote:

what I don't see is how to detect that I've fetched the last row from a
query. Is there more complete doco on this process somewhere?

You could either handle a NOT FOUND exception, or make use of SQLCODE or
SQLSTATE like this...

EXEC SQL DECLARE csr_fred CURSOR FOR SELECT....

EXEC SQL OPEN csr_fred;

while (SQLCODE == 0) {
EXEC SQL FETCH csr_fred INTO :jim, :sheila,...

if (SQLCODE == 0) {

....
body of row processing here
....
}
}

EXEC SQL CLOSE csr_fred;