RefLib

Reference library for AI

Back

postgresql_16:5.3.3.2 Using Cursors

PostgreSQL 16 > Client Interfaces > ECPG — Embedded SQL in C > Running SQL Commands > Using Cursors

To retrieve a result set holding multiple rows, an application has to declare a cursor and fetch each row from the cursor. The steps to use a cursor are the following: declare a cursor, open it, fetch a row from the cursor, repeat, and finally close it.

Select using cursors: EXEC SQL DECLARE foo_bar CURSOR FOR SELECT number, ascii FROM foo ORDER BY ascii; EXEC SQL OPEN foo_bar; EXEC SQL FETCH foo_bar INTO :FooBar, DooDad; ... EXEC SQL CLOSE foo_bar; EXEC SQL COMMIT;

For more details about declaring a cursor, see (see postgresql_16:5.3.14.4); for more details about fetching rows from a cursor, see (see postgresql_16:7.1.147).

Note

The ECPG DECLARE command does not actually cause a statement to be sent to the PostgreSQL backend. The cursor is opened in the backend (using the backend's DECLARE command) at the point when the OPEN command is executed.

Back