The For Loop Pl/sql language of the/* cursor provides the cursor's for loop statement, "Automatically performs the function of the cursor's open,fetch,close statement and circular statement," and when entering the loop, the cursor for Loop statement automatically opens the cursor and extracts the data from the first row of cursors.
When the program completes the current extracted data and enters the next loop, the cursor for Loop statement automatically extracts the next row of data for the program to process, when all the data rows in the result set end the loop and the cursor is automatically closed; For index_variable in cursor_name[(Value[,value] ...)]
Loop--Cursor processing code end loop; Where index_variable is the index variable implied by the cursor for loop statement, the variable is a record variable, the structure is the same as the collection of structures returned by the cursor query statement, and the extracted cursor data can be read by referencing the index record variable element in the program, Index_
The names of the elements in the variable are the same as the column names in the selection list of the cursor query statement, and if computed columns exist in the selection list of the cursor query statement, you must specify aliases for the computed columns to access the column data through the index variables in the cursor for loop statement;
:: Do not manually manipulate cursors in your program, and do not define records in your program that control the FOR loop:: * * DECLARE CURSOR c_sal is select emp.empno,emp.ename,emp.sal from EMP; Begin for V_sal in C_sal Loop--implicitly open cursor: Implied monitoring c_sal%notfound: implicitly close cursor Dbms_output.put_line (to_char (v_sal.empno) | | ....'|| v_sal.ename| |
To_char (v_sal.sal));
End Loop;
End
/*:: When the declared cursor with parameters, through the cursor for Loop statement for the cursor pass parameters:::: The cursor can be seen as a method, with parameters of the method * * DECLARE CURSOR c_cursor (dept_no number default 10)
is select Dept.dname,dept.loc from dept where Dept.deptno <= Dept_no; Begin Dbms_output.put_line (' when dept_nThe parameter of O is 30 o'clock: '); For C1_rec in C_cursor loop dbms_output.put_line (c1_rec.dname| | ': ' | |
C1_REC.LOC);
End Loop;
End
/* Pl/sql also allows the use of subqueries in cursor for loop statements to implement the function of a cursor/declare begin for C1_rec in (select Dept.dname,dept.loc from dept) loop Dbms_output.put_line (c1_rec.dname| | ' ...'||
C1_REC.LOC);
End Loop;
End