There are three situations in which the cursor participates in the Loop:
(1) Declare cursor --> open cursor --> loop ---> fetch curosr into --> end loop --> close cursor
In this case, every time fetch is called, The cursor will be moved to the next record, but we need to construct a loop by ourselves.
Declare
V_name TDZ. vxm% Type; -- % Type can be directly used to retrieve the field type
V_class TDZ. vbj% Type;
V_classes TDZ. vbj % type;
Cursor mycoursor is
Select vxm. vbj from TDZ where vbj = v_classes;
Begin
V_classes: = 'shanghai ';
---------------------------
Open mycoursor;
------------------------
Loop
FetchMycursorIntoV_name, v_class
Exit whenMycursor% Notfound;
End loop;
---------------------------------
Close mycursor;
--------------------------------
End;
(2) cursor For Loop
ForEmployee_recIn C1 --- direct use of employee_rec without prior definition
Loop
Total_val: = total_val +Employee_rec.Monthly_income;
End loop;
When Using cursor for loop, you do not need to manually open cursor to close cursor,
(3) Add the dulk collect batch processing to the cursor. The fetch statement extracts only one row from the result set at a time, and the cursor will
Point to the next row of the result set.
// Define the table first, because the batch processing of the cursor will return multiple records. we store the records in the table. The type of the object is the same as that of the field.
TypeTab_sal is table of EMP. Salary & % type;
TypeTab_idIs tableEMP. ID% Type; // EMP. ID (table name. Field name)
TypeTab_nameIs tableEMP. Name% Type;
Eid tab_id; // after the table is defined, you can use it to define variables to store the corresponding fields in the tag.
Ename tab_name;
Sales tab_sal;
Cursor my_curl is
Select emp_id, emp_name, salary from EMP whereRownum <= 3; (rownum is a pseudo column, indicating the current row)
Begin
-----------------------------------------------------------------------------
Open my_curl;
Fetch my_curl bulk collectEid, Ename, Sal;
----------------------------------------------------------------------------------
For I in 1 ..Eid. CountLoop -- I can be used directly
Dbms_output.put_line (ename (I); -- after batch processing is used, it can be the same as using arrays.
End loop;
---------------------------------------------------------------
Close my_curl;
------------------------------------------------------
End;
(Before rowcount is not set, my_curl will return more records, but rowcount can be used to limit it)
In addition, you can use limit in the fetch statement.
Fetch my_curl bulk collect into Eid, ename, SalLimit 3(You can also limit the number of records in a cursor record to a certain number)