RefLib

Reference library for AI

Back

postgresql_16:6.6.7.4 Looping through a Cursor's Result

PostgreSQL 16 > Server Programming > PL/pgSQL — SQL Procedural Language > Cursors > Looping through a Cursor's Result

There is a variant of the FOR statement that allows iterating through the rows returned by a cursor. The syntax is: <<*label*>> FOR *recordvar* IN *bound_cursorvar* ( *argument_name* := *argument_value* , ... ) LOOP *statements* END LOOP *label* ; The cursor variable must have been bound to some query when it was declared, and it *cannot* be open already. The FOR statement automatically opens the cursor, and it closes the cursor again when the loop exits. A list of actual argument value expressions must appear if and only if the cursor was declared to take arguments. These values will be substituted in the query, in just the same way as during an OPEN (see (see postgresql_16:6.6.7.2)).

The variable *recordvar* is automatically defined as type record and exists only inside the loop (any existing definition of the variable name is ignored within the loop). Each row returned by the cursor is successively assigned to this record variable and the loop body is executed.

Back