FOR SELECT
Loops row-by-row through a query result set
[label:] FOR <select_stmt> [AS CURSOR cursor_name] DO <compound_statement>
| Argument | Description |
|---|---|
label |
Optional label for |
select_stmt |
|
cursor_name |
Cursor name.It must be unique among cursor names in the PSQL module (stored procedure, stored function, trigger or PSQL block) |
compound_statement |
A single statement, or statements wrapped in |
The FOR SELECT statement
-
retrieves each row sequentially from the result set, and executes the statement or block of statements for each row.In each iteration of the loop, the field values of the current row are copied into pre-declared variables.
Including the
AS CURSORclause enables positioned deletes and updates to be performed — see notes below -
can embed other
FOR SELECTstatements -
can contain named parameters that must be previously declared in the
DECLARE VARIABLEstatement or exist as input or output parameters of the procedure -
requires an
INTOclause at the end of theSELECT … FROM …specification ifAS CURSORis absentIn each iteration of the loop, the field values of the current row are copied to the list of variables specified in theINTOclause.The loop repeats until all rows are retrieved, after which it terminates -
can be terminated before all rows are retrieved by using a
BREAK,LEAVEorEXITstatement