The Undeclared Cursor
The optional AS CURSOR clause surfaces the result set of the FOR SELECT structure as an undeclared, named cursor that can be operated on using the WHERE CURRENT OF clause inside the statement or block following the DO command, to delete or update the current row before execution moves to the next row.In addition, it is possible to use the cursor name as a record variable (similar to OLD and NEW in triggers), allowing access to the columns of the result set (i.e. cursor_name.columnname).
-
When accessing a cursor variable in a DML statement, the colon prefix can be added before the cursor name (i.e.
:cursor_name.columnname) for disambiguation, similar to variables.The cursor variable can be referenced without colon prefix, but in that case, depending on the scope of the contexts in the statement, the name may resolve in the statement context instead of to the cursor (e.g. you select from a table with the same name as the cursor).
-
Cursor variables are read-only
-
In a
FOR SELECTstatement without anAS CURSORclause, you must use theINTOclause.If anAS CURSORclause is specified, theINTOclause is allowed, but optional;you can access the fields through the cursor instead. -
Reading from a cursor variable returns the current field values.This means that an
UPDATEstatement (with aWHERE CURRENT OFclause) will update not only the table, but also the fields in the cursor variable for subsequent reads.Executing aDELETEstatement (with aWHERE CURRENT OFclause) will set all fields in the cursor variable toNULLfor subsequent reads
Other points to take into account regarding undeclared cursors:
-
The
OPEN,FETCHandCLOSEstatements cannot be applied to a cursor surfaced by theAS CURSORclause -
The cursor_name argument associated with an
AS CURSORclause must not clash with any names created byDECLARE VARIABLEorDECLARE CURSORstatements at the top of the module body, nor with any other cursors surfaced by anAS CURSORclause -
The optional
FOR UPDATEclause in theSELECTstatement is not required for a positioned update